0

I am attempting to spawn a new process with the following code:

String exec = (System.getProperty("user.dir") + "/privateers.jar");
        String[] command = {"javaw", "-jar", exec};  
        final Process process = Runtime.getRuntime().exec(command);
        System.out.println("Running " + exec);
        System.exit(0);

But for some reason, the process just is not running.

The "Running: " + exec prints out Running D:\Downloads\mcp\ship\ShipLauncher/privateers.jar

When I go to D:\Downloads\mcp\ship\ShipLauncher/, there is a valid file names "privateers.jar" and which works absolutely fine when I open it with a bat file:

java -jar privateers.jar
PAUSE

There are absolutely no errors in the console. enter image description here

Does anyone know the issue here? I cannot seem to find the problem.

Update: I tried using ProcessBuilder. but no luck.

String exec = (System.getProperty("user.dir") + java.io.File.separator + "privateers.jar");
        String[] command = {"java", "-jar", exec}; 
        ProcessBuilder pb =
                   new ProcessBuilder(command[0], command[1], command[2]);
        progressBar.setValue(100); 
        System.out.println(pb.command());
        pb.start();
        //final Process process = Runtime.getRuntime().exec(command);
        System.out.println("Running " + exec);
Joehot200
  • 1,070
  • 15
  • 44
  • Use `ProcessBuilder` instead – jmj Oct 29 '14 at 23:03
  • As you are using Windows, did you try do put a `\ ` instead of a `/` ? (`String exec = (System.getProperty("user.dir") + "\\privateers.jar");`) – Serge Ballesta Oct 29 '14 at 23:07
  • It may be exiting because its parent process is exiting. Try replacing `System.exit(0);` with `Thread.sleep(Long.MAX_VALUE);`. – VGR Oct 29 '14 at 23:08
  • unfortunately, you can't do it lke this. any forked processes will be killed when the parent process exits. you need an intermediate script to make this truly work. – jtahlborn Oct 29 '14 at 23:09
  • Start testing by reading the stdout from the process and using `waitFor` on the process and check the return (exit) value – MadProgrammer Oct 29 '14 at 23:09
  • see my answer here (for linux, you can do a similar thing with windows batch files using the "start" command) http://stackoverflow.com/questions/7665185/runtime-getruntime-exec-not-behaving-like-c-language-system-command – jtahlborn Oct 29 '14 at 23:12
  • @SergeBallesta Better would be `System.getProperty("user.dir") + java.io.File.separator + "privateers.jar"`. – Tom Oct 29 '14 at 23:14
  • Still looking for an answer. All comments so far have been useless. – Joehot200 Oct 30 '14 at 11:23
  • @MadProgrammer return value was 0. Nothing odd there. – Joehot200 Oct 30 '14 at 11:26
  • @jtahlborn I am fine with not killing the parent process, I guess. EDIT: not killing the parent process does nothing to fix the issue. – Joehot200 Oct 30 '14 at 11:27
  • @JigarJoshi Tried that, no change. – Joehot200 Oct 30 '14 at 11:32
  • @jtahlborn What? How does this relate at all to my problem? – Joehot200 Oct 30 '14 at 11:32
  • @jtahlborn: Not true. On Unix, when a parent process dies, child processes get reparented to the `init` process. On Windows, no such thing as `fork` to begin with. – Thomas Oct 30 '14 at 11:35
  • Can you try running the .bat file from within java instead and seeing what happens? – David ten Hove Oct 30 '14 at 11:43
  • @Thomas - i know how unix works, however, that is not how _java_ works. Java does not allow children to live longer than the parent. I know this because we need to do this for our product on both windows and linux, and the only way to get it to work is to use an intermediate script. (and i was using the word "fork" in the generic OS sense, not the posix function sense). – jtahlborn Oct 30 '14 at 12:55
  • I would suggest actually checking the standard output and error streams of the launched process as well as the exit code. _one_ of these should give some indication of the problem. – jtahlborn Oct 30 '14 at 12:58

0 Answers0