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).
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.
I get the error
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.
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 开发者_如何学Gop = processBuilder.start();
Edit 2
String[] commands = new String[]{
"C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe", "-d", "\"" + tempDir + "\"", "-classpath", classpath
};
Edit 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",
};
The string array that you pass to ProcessBuilder should contain one argument per array element, not everything in a single big string.
Try this:
String[] commands = new String[] { "C:\\Windows\\System32\\cmd.exe", "/c", "C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe" };
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" );
You have to read from process.getInputStream()
yourself. As far as I know, processes' output doesn't automatically show on stdout.
You're using the wrong method of process builder. Use the single string version, i.e. don't pass a string array, just pass a string. The string array version is for when you've already already divided up the command into the program, its options, and its the arguments. As it stands now, its looking for a program executable file called C:\\Windows\\System32\\cmd.exe /c C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe
.
Alternatively, split your command into the program and arguments in the string array and then you can use the String array version of process builder.
String[] = new String[] {
"C:\\Windows\\System32\\cmd.exe",
"/c",
"C:\\\"Program Files\"\\Java\\jdk1.6.0_23\\bin\\javac.exe"
}
And
精彩评论