开发者

Start Java main in another process (VM)

开发者 https://www.devze.com 2023-01-14 00:31 出处:网络
I have a server client project and for testing purposes I want to start the server in a whole new process. The problem is, I have just a main() method in project, no jar. So my guess would be somethin

I have a server client project and for testing purposes I want to start the server in a whole new process. The problem is, I have just a main() method in project, no jar. So my guess would be something like

Runtime.getRuntime().exec("javac MyServer.java");
Runtime.getRuntime().exec("java class MyServer");

But I am really not sure about it and also I don't exactly like the need to start javac for each test case.

How should I proceed?

EDIT

I want to start the process in the @Before method and destroy it in @After. It has to run automatically, so manual turning on the server is not an option. Wha开发者_如何学Ct I was looking for is a way to eliminate compilation of the server class. But now I guess there is no other way.


If this is for testing purposes, just launch the other process from the command line or use Eclipse to take care of it. In Eclipse the same project can have multiple main() entry points. When you wish to run the app you create a run / debug configuration that says which entry point you wish to invoke. So you could define one for the client and one for the server and run them with a button click.

Expanded:

Prerequisite - import your project into Eclipse first before doing any of this.

  1. Run Eclipse from the Java perspective (which is normally the case for a Java project)
  2. You will see two toolbar buttons marked Debug As... and Run As.... I will describe the Debug button from now on but the same principle applies to Run As..
  3. Next to the Debug As... button is a drop down button. Click it and from the drop down choose Debug Configurations...
  4. A configuration dialog will open. In the Dialog double click on "Java Application". Eclipse will create a new debug configuration for debugging a Java application.
  5. Use the fields on the right to choose which Eclipse project you are debugging, the main class (i.e. the one with the static main you wish to invoke) and any other args you want to set. You can give your configuration a meaningful name and apply the changes.
  6. Create two configurations one for your client and one for your server, e.g. "debug client" and "debug server"
  7. Exit the dialog
  8. Now the Debug As... drop down contains two actions with your new configurations. You can click them to launch your apps in debug mode. You will observe that each instance is running in a separate java.exe process.

Eclipse also has a debug perspective where you can stop / pause running processes, set breakpoints and whatnot. It can even debug more than one thing simultaneously so you could launch both client and server in debug and set breakpoints either side of the call.


Java allows you to make system calls like so.

 Runtime r = Runtime.getRuntime();
 Process p = r.exec("java otherMain arg0 arg1");

This would allow you to start another java process. Process also has methods to get the output of that process if you need it.


Take a look at JUnit, what I think you want to do is run a series of tests against your client, with a server running. You can accomplish this by writing a JUnit test which makes use of the @Before annotation to run a setup method before each test. For example:

import org.junit.Before

public class TestClient {
    private static final int port = 20000 + new Random().nextInt(10000);
    private MyServer server;

    @Before
    public void setup() {
        // setup server
        server = new MyServer(port);
        server.start();
    }

    @Test
    public void testX() {
        // test x
    }

    @Test
    public void testY() {
        // test y
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号