将 .jar 文件导出到临时文件
我有两个程序“a”和“b”机器人编译为 fatJar 文件。 “b”作为资源添加到“a”。我现在希望“a”使用流程生成器执行“b”。据我了解,我不能直接向进程构建器提供“b”jar,因为它在常规 Windows 文件系统中不存在。 建议的解决方案是访问“b”作为资源并将其内容写入临时文件,然后将临时文件交给进程构建器。
我的问题是:如何将 fatJar“b” 复制到临时文件中以使其保持可执行状态? 我当前的方法如下所示:
try {
Path tempFile = Files.createTempFile("b", ".jar");
InputStream stream = Main.class.getClassLoader().getResourceAsStream("b.jar");
try (InputStreamReader streamReader =
new InputStreamReader(stream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader)) {
String line;
while ((line = reader.readLine()) != null) {
Files.write(tempFile, reader.readLine().getBytes(StandardCharsets.UTF_8));
}
} catch (IOException e) {
e.printStackTrace();
}
ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", tempFile.getFileName().toString());
processBuilder.directory(new File(tempFile.getParent().toString()));
Process process = processBuilder.start();
processBuilder.redirectErrorStream(true);
System.out.println(process.isAlive());
process.isAlive() 调用确认进程正在运行,但是该进程没有按应有的方式运行(它应该通过输入和输出流建立双向通信,但我不会得到运行过程中的任何答案)。 有谁知道是否有更好的方法来解决整个情况,或者我将 .jar 复制到临时文件的方法是否错误?
I have two programms "a" and "b" bot compiled to fatJar files. "b" is added as resource to "a". I now want "a" to execute "b" using a process builder. As far as I understand it I cannot directly give the process builder the "b" jar for it doesn't exist in the regular windows file system.
The suggested solution to this is to access "b" as resource and write its content to a temp file and then hand the temp file to the process builder.
My question is: how do i copy the fatJar "b" into a temp file in a way it stays executionable?
My current approach looks like this:
try {
Path tempFile = Files.createTempFile("b", ".jar");
InputStream stream = Main.class.getClassLoader().getResourceAsStream("b.jar");
try (InputStreamReader streamReader =
new InputStreamReader(stream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader)) {
String line;
while ((line = reader.readLine()) != null) {
Files.write(tempFile, reader.readLine().getBytes(StandardCharsets.UTF_8));
}
} catch (IOException e) {
e.printStackTrace();
}
ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", tempFile.getFileName().toString());
processBuilder.directory(new File(tempFile.getParent().toString()));
Process process = processBuilder.start();
processBuilder.redirectErrorStream(true);
System.out.println(process.isAlive());
The process.isAlive() call confirms the process is running however the process doesn't function as it should (it should build up a two way communication via the input and output streams but i won't get any answer from the running process).
Does anyone know if there is an better approach to the whole situation or if my way of copying the .jar to the temp file is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,同时我想通了。事实证明,缓冲读取器导致了问题。下面的代码现在工作正常。
Ok so meanwhile i figured it out. Turns out the Buffered reader caused the problem. The below code works fine now.