Note that there are some explanatory texts on larger screens.

plurals
  1. POProcessBuilder - Start another process / JVM - HowTo?
    primarykey
    data
    text
    <p>I'm writing a network app, where each Client has a Singleton ClientManager. For <em>testing</em>, I would like to create several clients (each in their own VM / process) without starting the program by hand n-times.</p> <p>The following two questions on stackoverflow already describe how-to do that:</p> <ul> <li><a href="https://stackoverflow.com/questions/1229605/is-this-really-the-best-way-to-start-a-second-jvm-from-java-code">Is <em>this</em> really the best way to start a second JVM from Java code?</a> </li> <li><a href="https://stackoverflow.com/questions/636367/java-executing-a-java-application-in-a-separate-process/723914#723914">Java: Executing a Java application in a separate process</a></li> </ul> <p>My Code is based on these, but it's not working:</p> <ul> <li>The main program doesn't continue after spawn is called. </li> <li>The spawned code doesn't get executed.</li> </ul> <p>Here's the complete code using <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/lang/ProcessBuilder.html" rel="nofollow noreferrer">ProcessBuilder</a>:</p> <pre><code>public class NewVM { static class HelloWorld2 { public static void main(String[] args) { System.out.println("Hello World"); System.err.println("Hello World 2"); } } public static void main(String[] args) throws Exception { startSecondJVM(HelloWorld2.class, true); startSecondJVM(HelloWorld2.class, false); System.out.println("Main"); } public static void startSecondJVM(Class&lt;? extends Object&gt; clazz, boolean redirectStream) throws Exception { System.out.println(clazz.getCanonicalName()); String separator = System.getProperty("file.separator"); String classpath = System.getProperty("java.class.path"); String path = System.getProperty("java.home") + separator + "bin" + separator + "java"; ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", classpath, clazz.getCanonicalName()); processBuilder.redirectErrorStream(redirectStream); Process process = processBuilder.start(); process.waitFor(); System.out.println("Fin"); } } </code></pre> <p>What am I doing wrong???</p> <p>Btw:</p> <ul> <li>I'm using Eclipse. </li> <li>The Singleton problem is a <em>simplified</em> example. Please do <em>not</em> suggest creating a factory.</li> </ul> <p><strong>Solution:</strong> HelloWorld2 mustn't be an inner class.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload