我目前正在使用Java制作firefox插件开发GUI工具。但是,在尝试输出.bat
文件时,我陷入困境。
当我使用java运行.bat
文件时,我可以看到输出,但是在bat文件中有3个命令。当第一个命令执行时,我可以同时获得输出。但是当它执行第二个命令输出时没有来。当.bat
文件存在时,我得到的所有输出都没有同时出现。
我在执行时立即获得输出:
call "C:\mozilla-build\addon-sdk-1.16\bin\activate.bat
但是我没有同时获得以下命令的输出:
call cfx run
但我知道它正在执行,因为firefox窗口会弹出。执行proc.destroy();
这是我的蝙蝠档案:
@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();
如何立即获得输出以及为什么它对此命令的作用不同?
答案 0 :(得分:0)
我能够使用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();
}