0

I have a program that uses ProcessBuilder in order to run a different program. My code looks like this:

public class runMultiClient {
public static void main(String[] args){
    if (args[0].matches("-f")){
        String dir = System.getProperty("user.dir");
        String path = dir + "\\" + args[1];
        FileReader fr;
        try {
            fr = new FileReader(path);
            BufferedReader bf = new BufferedReader(fr);

            String line = "";
            Process PR = null;
            while ((line = bf.readLine()) != null){
                String[] tk = line.split(" ");
                String[] cmd = {"javaw", "-jar", "ntripClient.jar", "-a", tk[0], "-p", tk[1],
                        "-u", tk[2], "-pw", tk[3], "-m", tk[4], "-t", tk[5], "-s", tk[6]};
                ProcessBuilder pb = new ProcessBuilder(cmd);
                PR = pb.start();
            }
            PR.waitFor();
        } 
        catch (FileNotFoundException ex) {ex.printStackTrace();} 
        catch (IOException ex) {ex.printStackTrace();} 
        catch (InterruptedException ex) {ex.printStackTrace();}
    }
}}

I run this program from cmd. It excepts a name for the initiation file which contains data in each row. Based on this file it suppose create several processes.

However, it does not run smoothly. Right now i have 2 rows in the initiation file, meaning two processes should have been started. What it actually did is start the first process, then stopped it and started the second one instead. Another problem is that i do not know how to kill the process, it seems to be running in the background and does not appear in the task manager. Crtl+C and Ctrl+Break does not work.

The main goal is to run several processes in the same time and being able to break the process when needed. What am i missing? how do i fix this issue? Any help would be appreciated.

Xyz
  • 5,955
  • 5
  • 40
  • 58

1 Answers1

0

Without being able to reproduce this (for lack of the subject JAR), one of three things is probably happening:

  • The processes are short-lived and come down after a short while (I'm going to guess that's not the case given the phrasing in your question), or
  • The processes are not starting up properly to begin with, or
  • The child processes are dying when the parent process reaches the end of its main method

Some things to try:

  • pb.start() will return a Process object. Once all of your processes are up, have your main method wait for one (or all) of them to finish and see if the children are still alive.
  • Double-check that you have no exceptions in the logs. Using printStackTrace rather than System.out.println could make this easier for you to spot.
Joe C
  • 15,324
  • 8
  • 38
  • 50
  • What do you mean by short-lived? when the process are working it is constantly pulling data from some server and placing files into directories on my computer. Should i create a different name process builder for each one? when waiting for the process what do i actually wait for? –  Sep 10 '16 at 10:35
  • Also the `ntripClient.jar` has some console outputs that should be printed out. Why are they not being printed? When i run `ntripClient.jar` individually it works fine and prints out the outputs to the cmd window. –  Sep 10 '16 at 10:37
  • [Process.waitFor()](https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#waitFor()) will wait for the child process to finish. Adding this to the end of your main method will tell us whether the children are being killed when the parent dies or whether it's an issue actually starting up the process. The waitFor() method will also give you the return code of the process when it dies, which may also help with your debugging. – Joe C Sep 10 '16 at 10:43
  • I understand, but how would the process be finished if it keeps reading data from the server? it just keep running permanently. –  Sep 10 '16 at 10:45
  • I have added the `Process.waitfor();` command. Now it only started the first sub process (i had two streams in the input file). It never reached the second one. –  Sep 10 '16 at 10:51
  • So the fact that a process is running is an improvement, and it tells me that the ending of your main process is killing your children. Moving the `Process.waitFor()` outside of your loop should bring them all up. – Joe C Sep 10 '16 at 20:47
  • I have edited the code as you suggested. Still, it does not run properly. It starts both of the processes, but only one continues running normally, the other one (the first one) is stuck and does not performs like it should. –  Sep 11 '16 at 07:06
  • 1
    I'm not sure that's the reason, but some programs (java might be one of them) stop running if the Sysout and Syserr streams are not connected (when the programs tries to output something it just hangs waiting for some other process to pull the program output). I suggest you try connecting Sysout and Syserr streams, maybe using this method: http://stackoverflow.com/a/14168097/1140972 – iddo Sep 11 '16 at 11:01