问题描述
我正在尝试使用ProcessBuilder从Java执行javac,但我没有输出,也没有任何反应。我尝试读取输入流(因为如果我不读它就会出现进程挂起的错误),但仍然没有结果。我最初将所有必需的参数传递给javac,但它没有工作,所以我把它简化为javac(应该打印帮助信息)。
I'm trying to execute javac from Java using ProcessBuilder but i get no output and nothing happens. I tried reading the input stream (as there is a bug where the process hangs if i don't read it) but still no results. I originally passed all required parameters to javac but it was not working, so i simplified it down to just javac (which should print the help message).
我试过运行
C:\ Windows \ System32 \ cmd.exe / c C:\\Program Files \\ Java \ jdk1.6.0_23 \ bin\javac.exe
C:\\程序文件\\ Java \ jdk1.6.0_23 \ bin \ javac.exe
并用双引号包围javac的整个路径但仍然什么都没有。
i tried running"C:\Windows\System32\cmd.exe /c C:\\"Program Files\"\Java\jdk1.6.0_23\bin\javac.exe""C:\\"Program Files\"\Java\jdk1.6.0_23\bin\javac.exe"and surrounding the entire path to javac with double quotes but still nothing.
我收到错误
无法运行程序C:\ Windows \ System32 \ cmd.exe / c C:\Program Files\ Java \ jdk1.6.0_23\bin\javac.exe:CreateProcess error = 2,系统找不到指定的文件
Cannot run program "C:\Windows\System32\cmd.exe /c C:\"Program Files"\Java\jdk1.6.0_23\bin\javac.exe": CreateProcess error=2, The system cannot find the file specified
但如果我复制命令并从命令行运行它可以正常工作。
but if i copy the command and run it from the command line it works fine.
我知道使用JavaCompiler类来编译我的文件,但我宁愿首先修复此问题,因为我可以从Java运行任何dos应用程序或.bat文件。我可以运行像notepad.exe这样的GUI程序。
I am aware of using the JavaCompiler class to compile my files but i would prefer to get this problem fixed first as i can't run any dos application or .bat file from Java. I can run GUI programs like notepad.exe fine though.
String[] commands = new String[]{
"C:\\Windows\\System32\\cmd.exe /c C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe"
};
logger.log(Level.INFO, "About to run javac with the command below:");
String commandToOutput = "";
for (String command : commands) {
commandToOutput += command + " ";
}
logger.log(Level.INFO, commandToOutput);
ProcessBuilder processBuilder = new ProcessBuilder(commands);
Process p = processBuilder.start();
编辑2
String[] commands = new String[]{
"C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe", "-d", "\"" + tempDir + "\"", "-classpath", classpath
};
编辑3
为什么会这样第二个命令数组有效,但第一个不在下面。
why is it that the second commands array works but the first does not below.
//this gives me CreateProcess error=5, Access is denied
commands = new String[]{
"C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe"
};
//this works
commands = new String[]{
"C:\\Windows\\System32\\cmd.exe",
"/c",
"C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe",
};
推荐答案
传递给ProcessBuilder的字符串数组应包含一个每个数组元素的参数,而不是单个大字符串中的所有内容。
The string array that you pass to ProcessBuilder should contain one argument per array element, not everything in a single big string.
试试这个:
String[] commands = new String[]
{
"C:\\Windows\\System32\\cmd.exe",
"/c",
"C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe"
};
顺便说一句:没有必要调用cmd.exe,你可以直接将javac.exe传递给ProcessBuilder
Btw: there is no need to call cmd.exe, you can pass javac.exe directly to the ProcessBuilder
ProcessBuilder builder = new ProcessBuilder(
"C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe", "\\Path\\To\\MyClass.java"
);
这篇关于无法使用Windows 7下的ProcessBuilder在Java中执行javac或其他命令行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!