views:

82

answers:

3

Hi, 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. What I was looking for is a way to eliminate compilation of the server class. But now I guess there is no other way.

+2  A: 

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.

locka
Ok, so assuming this would start a new JVM (which I don't exacly believe, but don't know), I am really new to exclipse and can't find in run configurations, what you are talking about. Can you please direct me a bit more?
Trimack
I've updated my response with steps for how to debug in Eclipse. Eclipse can spawn as many instances of Java as you would like it to. It can even spawn J2EE app servers and even allows you to specify which JRE you wish your app to run against but those are more advanced topics.
locka
I see now what you mean, but that's not exactly what I am looking for. I want to start a new server for each test and then kill it. This way I would die clicking. However, thanks anyway (vote up).
Trimack
A: 

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.

mR_fr0g
Which is pretty much what I have written in the question minus the need to compile the class, or is it?
Trimack
A: 

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
    }
}
Jon Freedman
You are correct, I will update the question, but in the Before method I want to start a new virtual machine with the server. That's where the question is going
Trimack
Why do you need a new VM? Are you absolutely sure thats necessary?
Jon Freedman
My boss demands it :) It is necessary.
Trimack
In that case you can go with your original idea of running java via `Runtime.exec` - there's nothing stopping you from calling that in a @Before method. As long as your `MyServer` class is in the same project you won't need to re-compile it, as all your code should be compiled before running tests.
Jon Freedman