Can I spawn multiple different JVMs given the same Main.class, arguments and VM options? Is it possible to do it with the ProcessBuilder?
-
nice question , i am too curious nowPanther– Panther2014-12-11 13:30:45 +00:00Commented Dec 11, 2014 at 13:30
-
Me too. I found some sample code here and there but nothing that actually worked :(Virginia Woolf– Virginia Woolf2014-12-11 13:34:04 +00:00Commented Dec 11, 2014 at 13:34
-
With "different JVM" do you mean multiple instances of the same JVM installation or a JVM 1.6, JVM 7, JVM 8, etc.?Korashen– Korashen2014-12-11 13:46:54 +00:00Commented Dec 11, 2014 at 13:46
-
Multiple instances of the same JVM installation.Virginia Woolf– Virginia Woolf2014-12-11 13:51:21 +00:00Commented Dec 11, 2014 at 13:51
Add a comment
|
1 Answer
Here is a basic example using Process that starts 10 different JVM process:
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
public void run() {
try {
//start a new jvm with 256m of memory with the MyClass passing 2 parameters
String cmd = "java -Xmx256M -cp myjar.jar com.mymainclass.MyClass par1 par2";
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = br.readLine();
while (line != null) {
line = br.readLine();
}
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
line = br.readLine();
while (line != null) {
line = br.readLine();
}
} catch (IOException e) {
}
}
}).start();
}
3 Comments
Virginia Woolf
So I can't take advantage of the fact that I m doing all this from within the same jar in order not have to reference it this way and copy all environment parameters and arguments?
Gilberto Torrezan
You probally should use the Apache Commons Exec to prevent common mistakes when executing external processes from Java.
fmodos
@KaterinaA. the process is a clean new environment, it wont use anything of the current environment... this is the same with you open a prompt and enter de command