gpt4 book ai didi

java - Runtime.getRuntime 未能同时但稍后获得输出

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:47 26 4
gpt4 key购买 nike

我目前正在使用 Java 制作 Firefox 插件开发 GUI 工具。但是,当我尝试获取 .bat 的输出时,我陷入了困境。文件。

当我运行.bat时file using java 我可以看到输出,但是bat文件中写入了3条命令。当第一个命令执行时,我可以同时获得输出。但是当它执行第二个命令时,输出没有出现。当 .bat文件存在我得到了所有不同时出现的输出。

执行时我立即得到输出:

call "C:\mozilla-build\addon-sdk-1.16\bin\activate.bat

但是我没有同时获得以下命令的输出:

call cfx run

但我知道它正在执行,因为 Firefox 窗口弹出了。当我执行 proc.destroy(); 时,我突然得到所有输出

这是我的bat文件:

@echo off
call %1
cd C:\Users\Madhawa.se\Desktop\workingfox\beauty
call cfx run
pause

这是我的 Java 代码:

Thread t = new Thread(new Runnable() {

@Override
public void run() {
try {
Runtime rt = Runtime.getRuntime();

String[] commands = {"C:\\Users\\Madhawa.se\\Desktop\\workingfox\\runner\\foxrun.bat", "C:\\mozilla-build\\addon-sdk-1.16\\bin\\activate.bat"};

proc = rt.exec(commands);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

// read the output from the command

String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}

// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
proc.waitFor();
System.out.println("success");
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
t.start();

如何立即获取输出以及为什么该命令的行为不同?

最佳答案

我能够使用process builder而不是runtime.exec修复它。并且inheriteIo不起作用。它阻止了实时输出

Thread t = new Thread(new Runnable() {
private String s;

@Override
public void run() {
try {

Component selectedComponent = jTabbedPane2.getSelectedComponent();
if (selectedComponent instanceof MyTextArea) {
String response = "";
System.out.println("yes");
MyTextArea temptextarea = (MyTextArea) selectedComponent;
String xpiPath = new File(temptextarea.getNameX()).getParentFile().getPath();

String[] commands = {"C:\\Users\\Madhawa.se\\Desktop\\workingfox\\runner\\foxrun.bat", "C:\\mozilla-build\\addon-sdk-1.16\\bin\\activate.bat

ProcessBuilder process = new ProcessBuilder(commands);
process.redirectErrorStream(true);

Process shell = process.start();

//shell.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(shell.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(shell.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println("s:" + s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println("w:" + s);
}
}

} catch (Exception ex) {
ex.printStackTrace();
}

关于java - Runtime.getRuntime 未能同时但稍后获得输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com