Run Executable In Jar With ProcessBuilder [duplicate]

早过忘川 提交于 2019-12-01 09:11:21

问题


I have an application I've built that uses a non-Java executable that it calls via ProcessBuilder:

ProcessBuilder pb = new ProcessBuilder(invocation);
pb.redirectErrorStream(true);
Process proc = pb.start();
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

However, I'd like to bundle that app neatly into the jar file itself, instead of requiring it be placed outside in the same directory. Is there a way to run this app without extracting it?

If I need to drop ProcessBuilder, that would be fine, as long as it works. :)


回答1:


You'd essentially be asking the OS to run an application inside of a ZIP file. I'm not aware of any way to do that without extracting it first. Even if it were a Java application, you'd need to invoke the java executable, since it knows how to parse the JAR file and access classes and resources within it.

The only fairly easy workaround I can think of would be to extract the contents of the JAR to a temporary folder and run the program from there. This can be done programmatically using the classes in java.util.zip.




回答2:


You could just bundle the executable and JAR into another zip. That way, when it is extracted by the user, everything is where you expect it to be.



来源:https://stackoverflow.com/questions/1336055/run-executable-in-jar-with-processbuilder

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