如何从另一个 jar 运行 一个 jar 并在执行期间显示第一个 runnable 的控制台输出
How to Run a jar from another jar and show the console output from the first runnable during the execution
我是 运行 来自另一个 jar 的 jar 文件,例如 here somebody answers 但正在等待处理。
Process proc = Runtime.getRuntime().exec("java -jar A.jar" + stringParams);
try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
当我没有关于被调用程序状态的反馈时,我的问题就来了,但我不希望我的程序继续超出这些范围。我需要标准和错误输出,但结果会在执行结束时显示。有什么方法可以在 jar 仍然 运行 时执行和获取这些流吗?
Buffered/unbuffered
这似乎是 缓冲输出 的问题。
执行的进程(在本例中 java -jar <path>
)缓冲输出并仅在完成时写入(大块,我们不喜欢这样!)
所以一种方法是通过取消缓冲(非常 hacky 的工具)执行进程:
unbuffered <command>
stdbuf -i0 -o0 -e0 <command>
- 使用终端仿真
黑客攻击
stdbuf
是 GNU 工具的一部分。
https://www.gnu.org/software/coreutils/manual/html_node/stdbuf-invocation.html
unbuffered
是 expect
包的一部分。
关键是让程序认为它处于交互模式(就像您从控制台启动它一样)。
前两个选项非常老套,并非在所有情况下都有效(idk if java 命令对它们有效?)
仿真
第三个选项最有希望。
我们启动了一个模拟交互式终端的程序(终端模拟器),使程序认为它在真实的活动会话中工作!
Pty4j
您也可以使用 pty4j
:
从那里:https://github.com/traff/pty4j
// The command to run in a PTY...
String[] cmd = { "java", "-jar", "path_to_jar" };
// The initial environment to pass to the PTY child process...
String[] env = { "TERM=xterm" };
PtyProcess pty = PtyProcess.exec(cmd, env);
OutputStream os = pty.getOutputStream();
InputStream is = pty.getInputStream();
// ... work with the streams ...
// wait until the PTY child process terminates...
int result = pty.waitFor();
// free up resources.
pty.close();
Zt-exec
也许值得一试 zt-exec
?
我不知道它是如何执行命令的。
但可能是这样(我没有测试过)。
使用https://github.com/zeroturnaround/zt-exec
new ProcessExecutor().command("java", "-jar path_to_jar")
.redirectOutput(new LogOutputStream() {
@Override
protected void processLine(String line) {
...
}
})
.execute();
应该可以,但我没有测试过。
一般来说,没有办法很好的解决你的问题。
根据您要针对的平台考虑使用 unbuffered
、stdbuff
或(最慢的)终端仿真...
如果有帮助请告诉我,祝你好运! :)
我是 运行 来自另一个 jar 的 jar 文件,例如 here somebody answers 但正在等待处理。
Process proc = Runtime.getRuntime().exec("java -jar A.jar" + stringParams);
try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
当我没有关于被调用程序状态的反馈时,我的问题就来了,但我不希望我的程序继续超出这些范围。我需要标准和错误输出,但结果会在执行结束时显示。有什么方法可以在 jar 仍然 运行 时执行和获取这些流吗?
Buffered/unbuffered
这似乎是 缓冲输出 的问题。
执行的进程(在本例中 java -jar <path>
)缓冲输出并仅在完成时写入(大块,我们不喜欢这样!)
所以一种方法是通过取消缓冲(非常 hacky 的工具)执行进程:
unbuffered <command>
stdbuf -i0 -o0 -e0 <command>
- 使用终端仿真
黑客攻击
stdbuf
是 GNU 工具的一部分。
https://www.gnu.org/software/coreutils/manual/html_node/stdbuf-invocation.html
unbuffered
是 expect
包的一部分。
关键是让程序认为它处于交互模式(就像您从控制台启动它一样)。
前两个选项非常老套,并非在所有情况下都有效(idk if java 命令对它们有效?)
仿真
第三个选项最有希望。 我们启动了一个模拟交互式终端的程序(终端模拟器),使程序认为它在真实的活动会话中工作!
Pty4j
您也可以使用 pty4j
:
从那里:https://github.com/traff/pty4j
// The command to run in a PTY...
String[] cmd = { "java", "-jar", "path_to_jar" };
// The initial environment to pass to the PTY child process...
String[] env = { "TERM=xterm" };
PtyProcess pty = PtyProcess.exec(cmd, env);
OutputStream os = pty.getOutputStream();
InputStream is = pty.getInputStream();
// ... work with the streams ...
// wait until the PTY child process terminates...
int result = pty.waitFor();
// free up resources.
pty.close();
Zt-exec
也许值得一试 zt-exec
?
我不知道它是如何执行命令的。
但可能是这样(我没有测试过)。
使用https://github.com/zeroturnaround/zt-exec
new ProcessExecutor().command("java", "-jar path_to_jar") .redirectOutput(new LogOutputStream() { @Override protected void processLine(String line) { ... } }) .execute();
应该可以,但我没有测试过。 一般来说,没有办法很好的解决你的问题。
根据您要针对的平台考虑使用 unbuffered
、stdbuff
或(最慢的)终端仿真...
如果有帮助请告诉我,祝你好运! :)