ProcessBuilder or Runtime.getRuntime().exec won't complete jar execution

606 views Asked by At

I have a jar which converts one XML to other XML format using XSLT in Java. The jar copies the output to some folder. It is working absolutely fine when am running it on command prompt but running it via Runtime.getRuntime().exec or ProcessBuilder, doesn't complete the process. Just 25 files are converted and it freezes. When i shutdown the process i can see all the files being loaded in the output folder which were not being loaded into the same folder.

Any suggestions?

My Code

 private boolean runLoaderScript() throws IOException, InterruptedException {
    String args[] = { "java", "-jar", "C:\\Users\\gursahibsahni\\Desktop\\jar\\epnlm_new-1.0.0-jar-with-dependencies_WSJ_stringdate.jar", "-c", "-f", "-d", "7", "C:\\Users\\gursahibsahni\\Desktop\\ConsynInput\\wsjInput\\input" };
    ProcessBuilder builder = new ProcessBuilder(args);
    Process qq = (builder).start();
    qq.waitFor();
    return true;
}

private boolean runValidator() throws IOException, InterruptedException {
    Process validatorProcess = Runtime.getRuntime().exec("java -jar C:\\Users\\gursahibsahni\\Desktop\\jar\\wsj_jar_20140423.jar  -efv -d 7 C:\\Users\\gursahibsahni\\Desktop\\ConsynInput\\wsjInput\\output");
    return (validatorProcess.waitFor()) == 0 ? true : false;
}

Additionally, when am trying to import the jar in my project and call the main function to convert the XML, it is not converting the XML properly. Meaning, the constants are coming up very nicely but the functions which are being called into class files to get data are not being called up during the import.

YES! Running the jar on command line is a success! It works flawlessly. But when imported it is not converting properly. Why such behavior ? Its very strange. Please help.

1

There are 1 answers

2
André R. On BEST ANSWER

you have to consume the StdOut (and maybe StdErr) of your process ... otherwise the process will hang when the buffer is filled up !

if you don't want to code that yourself you might have a look at Apache CommonsExec ... it helps with executing and handling external Processes https://commons.apache.org/proper/commons-exec/tutorial.html

Among other things it captures the subprocesses output using an org.apache.commons.exec.ExecuteStreamHandler.