error in running a jar using java code

巧了我就是萌 提交于 2020-01-06 07:24:33

问题


I have a java swing application to extract text from pdf. It works fine when i run using command prompt(java -jar xyz.jar) or double click and run the jar but it gets stuck when I run using java code

Process asm = Runtime.getRuntime().exec("java -jar xyz.jar");
asm.waitFor();

or using process builder. is it because of the exceptions in my application? I'm not sure.


回答1:


You probably aren't in the proper directory.

Use the directory() method of ProcessBuilder so that the external process is run in the correct place so that the jar can be found.

ProcessBuilder processBuilder = new ProcessBuilder("java -jar xyz.jar");
processBuilder.redirectErrorStream(true);
processBuilder.directory(  complete this );
Process process = processBuilder.start();
String output = readOutput(process);
try {
    if (process.waitFor() != 0) {
        throw new IOException("command exited in error: " + process.exitValue() + "\n" + output);
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}

If this doesn't solve your problem, print the output streams (including the error one) to see what happens.



来源:https://stackoverflow.com/questions/11080224/error-in-running-a-jar-using-java-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!