1

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.

2
  • 1
    Why are you using exec in Java when you can just import the JAR file to classpath and do {CLASS}.main({ARGS}); (you can use reflection to import classes dynamically)?
    – nulldev
    Jun 17, 2015 at 17:09
  • Yeah, it did work. But why is the above method not working. ? Jun 18, 2015 at 6:19

1 Answer 1

0

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.

2
  • it is working.. can you please enlighten about filling up of the buffer? Jun 18, 2015 at 15:51
  • It has got nothing to do with java itself. The Operation System (in this respect there is no difference between Linux, Win, ...) creates a buffer which is just several kBytes in size between the parent and the child process. if your child process fills up this buffer by writing to the console and the parent is not consuming the data from the buffer then eventually the buffer is filled up and the childs next write to it will block and 'hang' the child process.
    – André R.
    Jun 19, 2015 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.