class FirstApp {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
SecondApp.main(args);
}
}).start();
Starts a new thread and calls the main method of another application. Works fine. But they run both in the same process.
However, if you want to do it as if it was executed from command line (in another (separate) process), you can also do something like this:
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
StringBuilder cmd = new StringBuilder();
cmd.append(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java ");
for (String jvmArg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
cmd.append(jvmArg + " ");
}
cmd.append("-cp ").append(ManagementFactory.getRuntimeMXBean().getClassPath()).append(" ");
cmd.append(Main2.class.getName()).append(" "); // Main2 is the main class of the second application
for (String arg : args) {
cmd.append(arg).append(" ");
}
Runtime.getRuntime().exec(cmd.toString());
// continue with your normal app code here
}
}
I took the second code mostly from How can I restart a Java application?