使用ProcessBuilder在Jar中运行可执行文件
我有一个我构建的应用程序,它使用通过ProcessBuilder调用的非Java可执行文件:
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);
但是,我想将该应用程序整齐地捆绑到jar中文件本身,而不是要求它放在同一目录之外。有没有办法运行这个应用程序而不提取它?
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?
如果我需要删除ProcessBuilder,那就没关系,只要它有效。 :)
If I need to drop ProcessBuilder, that would be fine, as long as it works. :)
你基本上要求操作系统在ZIP文件中运行一个应用程序。如果不首先提取它,我不知道有任何方法可以做到这一点。即使它是Java应用程序,您也需要调用 java 可执行文件,因为它知道如何解析JAR文件并访问其中的类和资源。
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.
我能想到的唯一相当简单的解决方法是将JAR的内容提取到临时文件夹并从那里运行程序。这可以使用 java.util.zip 中的类以编程方式完成。
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.