0

I have a Java program (in jar) which will invoke a batch file by passing parameters. The same batch file will be invoked 10 times with different values passing through parameter.

Which means the batch will be running in parallel with 10 instances.

The issue here is, all the process getting stopped at some point without any reason.

Please advise how to fix it.

    public static void run(String batpath)
    {
        try
        { 
            System.out.println("Call a batch file");    
            Process p= Runtime.getRuntime().exec("cmd /c CD D:\\ && cd "+v_Base_Path+" && "+batpath+" ");
            p.waitFor();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());         
        }   
    }
}
Divakar Ragupathy
  • 191
  • 1
  • 6
  • 20
  • 1
    This code won't run the processes in parallel. It will wait for each one, in turn, to terminate. You'd need to use `join()` or similar to run them in parallel then wait until they have all finished. – DNA Jul 09 '14 at 15:41
  • Are you seeing any exceptions? – Jamie Cockburn Jul 09 '14 at 15:42
  • 1
    As a way of troubleshooting, you might want to print out the process's error and output streams, in order to see why/if the process says it failed – clearlyspam23 Jul 09 '14 at 15:42
  • @DNA: My apologies, the original code doesnt have p.waitFor() command. It was added by me just before posting my question. So ignore that line. – Divakar Ragupathy Jul 09 '14 at 18:34
  • @JamieCockburn: No I didnt get any exception either in Java or in batch – Divakar Ragupathy Jul 09 '14 at 18:35
  • @clearlyspam23: I have added the a line in my batch to create log file and there is no exceptions logged. Its simply stopped. Not sure whether this issue due to Java memory issue or something else. However each batch process will run maximum of 2 to 3 hrs. – Divakar Ragupathy Jul 09 '14 at 18:38
  • I see a potential problem in your commands : you write `CD D:\\`. In DOS/Windows this command change the current directory for disk `D:` but **does not change disk**. You must use `CD /D D:\\` to change both. – Serge Ballesta Jul 09 '14 at 19:22

1 Answers1

0

Naturally, the first thing to check is whether your command cmd /C CD ... works correctly outside of Java.

Then, the recommended way to launch subprocesses is with ProcessBuilder:

The following is for Java 7 (see this answer for a Java 6 solution).

public class Processes
{
    public static void main(String... args)
    {
        for (int i = 0; i < 10; i++)
        {
            try
            {
                System.out.println("Call a batch file " + i);
                new ProcessBuilder("cmd", "/C", "echo", "hello").inheritIO().start();
                //new ProcessBuilder("bad", "/C", "echo", "hello").inheritIO().start();
            }
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
}

This enables you to print out the standard output and error streams, so you can see any output from your batch files. Without this, you won't see any clue as to what is wrong with the subprocesses.

If you use the commented-out "bad" line above instead of the "good" line, then you should see errors instead of "hello"s.

(If your batch files don't produce any output or error text, or are hanging forever, then there's nothing you can do at the Java level to debug and fix them - you have to look at them directly to find out why they are not working).

Your batch files may actually be hanging because you are not reading the output streams, as mentioned in the Process documentation:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

See the excellent (though now somewhat out-of-date now that Java 7 and 8 are out) When Runtime.exec() won't article for a detailed discussion.

Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
  • Thanks for the comment and sorry for getting back late. The batch file posting some 6000 xmls into my web application using Curl command. hence the batch will execute 6000 times hence it will take 2 hrs. So even if I use process builder I cant track at what stage it fails – Divakar Ragupathy Jul 19 '14 at 15:57
  • If you redirect the output to a file (and possibly use the `-v` option to make `curl` more verbose) you should be able to see how far it gets, and record any errors from `curl`. – DNA Jul 19 '14 at 18:11