Can't execute javac or other command line applications in Java using ProcessBuilder under Windows 7

南楼画角 提交于 2019-12-02 12:00:15

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!