简体   繁体   English

ProcessBuilder或Runtime.getRuntime()。exec无法完成jar执行

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

I have a jar which converts one XML to other XML format using XSLT in Java. 我有一个jar,可以使用Java中的XSLT将一个XML转换为其他XML格式。 The jar copies the output to some folder. jar将输出复制到某个文件夹。 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. 在命令提示符下运行它时,它的运行情况绝对良好,但是通过Runtime.getRuntime().exec or ProcessBuilder运行它并不能完成该过程。 Just 25 files are converted and it freezes. 仅转换25个文件,然后冻结。 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. 此外,当尝试将jar导入我的项目并调用main函数转换XML时,它不能正确转换XML。 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! 在命令行上运行jar是成功的! It works flawlessly. 它完美地工作。 But when imported it is not converting properly. 但是,导入时无法正确转换。 Why such behavior ? 为什么会有这种行为? Its very strange . 这很奇怪 Please help. 请帮忙。

you have to consume the StdOut (and maybe StdErr) of your process ... otherwise the process will hang when the buffer is filled up ! 您必须消耗进程的StdOut(甚至是StdErr)...否则,当缓冲区已满时,进程将挂起!

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 如果您不想自己编写代码,可以看看Apache CommonsExec ...它有助于执行和处理外部流程https://commons.apache.org/proper/commons-exec/tutorial.html

Among other things it captures the subprocesses output using an org.apache.commons.exec.ExecuteStreamHandler. 除其他事项外,它使用org.apache.commons.exec.ExecuteStreamHandler.捕获子流程输出org.apache.commons.exec.ExecuteStreamHandler.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM