简体   繁体   中英

Start another java program inside a java program and kill the mother process

I use terminal command "java -jar secondApp.jar" inside my java file to start a secondApp.jar.
I need secondApp.jar to run even if first app is killed.
This scenario works perfectly in windows environment. But when I test this in linux environment(Ubuntu 16.04) it seems that killing the first process kills the both processes.
This is the code I use to start the second app.

String command = "java -jar secondApp.jar"
Process process = Runtime.getRuntime().exec(command);

What am I doing wrong?

Prepare a batch file and a linux script file with the desired java command, then try this:

    if (SystemUtils.IS_OS_WINDOWS) {
        // run batch file
        String batchFullPath = new File("C:\\myBatchFile.bat").getAbsolutePath();
        Runtime.getRuntime().exec("cmd /C start " + batchFullPath);
    } else if (SystemUtils.IS_OS_LINUX) {
        // run linux script
        String scriptFullPath = new File("~/myScriptFile.sh").getAbsolutePath();
        File workingDir = new File("~");
        Runtime.getRuntime().exec("/usr/bin/xterm " + scriptFullPath, null, workingDir);
    } else {
        throw new RuntimeException("Unsupported Operating System");
    }

(Using xterm as it is fairly safe to assume every Linux machine has it installed)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM