2

Goal: I have client-server program in which client and server runs in different jvms. To test the same, I want to invoke the Server in a different JVM programatically and then use current jvm to run the client and execute different C/S tests.

Is there any way I can execute a method or run Java commands in different jvm programatically?

Force444
  • 3,321
  • 9
  • 39
  • 77
Vinay
  • 103
  • 1
  • 6

4 Answers4

4

1) The most powerful tool in java to run process is ProcessBuilder:

ProcessBuilder pb = new ProcessBuilder("java", "-server", "-jar", "yourJar.jar");

Process p = pb.start();

Than using Process you are able to manipulate child process e.g. read InputStream, destroy e.t.c.


2) If you are able to edit both source code review this question to build efficient communication between JVM on the same host. If you cannot change code, simply create own loader which load Server and implements inter JVM communication and invoke methods you need, because it in the same JVM space.

Community
  • 1
  • 1
Taky
  • 5,284
  • 1
  • 20
  • 29
1

You can run virtually any command which you otherwise run manually using

             Runtime.getRuntime().exec(command);

For more refer to Runtime.getRuntime().exec(...) documentation.

But also note that running any platform specific command using exec will rob your program its platform independent nature.

Sometime back I saw someone using "mv" to move a file. That made the entire program to Unix-based OS specific. Charm of Java or any virtual machine based language is its platform independent nature.

mawia
  • 9,169
  • 14
  • 48
  • 57
0

You can use command line:

 Runtime.getRuntime().exec("java -server MyServer")

or if you want to build some more complicated call just use http://commons.apache.org/exec/ to build and run program.

Koziołek
  • 2,791
  • 1
  • 28
  • 48
-1

From what I know, this is not possible with plain java. Probably grid enabled frameworks could provide a way of running a java program on multiple JVMs. A similar problem was resolved here:

how-to-run-a-java-file-project-in-remote-jvm-which-is-present-in-other-network

Community
  • 1
  • 1
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49