在 Windows 7 下无法使用 ProcessBuilder 在 Java 中执行 javac 或其他命令行应用程序 [英] Can't execute javac or other command line applications in Java using ProcessBuilder under Windows 7

查看:32
本文介绍了在 Windows 7 下无法使用 ProcessBuilder 在 Java 中执行 javac 或其他命令行应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ProcessBuilder 从 Java 执行 javac,但没有输出,也没有任何反应.我尝试读取输入流(因为如果我不读取它会导致进程挂起的错误),但仍然没有结果.我最初将所有必需的参数传递给 javac 但它不起作用,所以我将其简化为仅 javac(应该打印帮助消息).

我试过跑步"C:WindowsSystem32cmd.exe/c C:\"Program Files"Javajdk1.6.0_23injavac.exe""C:\"程序文件"Javajdk1.6.0_23injavac.exe"并用双引号包围 javac 的整个路径,但仍然没有.

我收到错误

Cannot run program "C:WindowsSystem32cmd.exe/c C:"Program Files"Javajdk1.6.0_23injavac.exe": CreateProcess error=2,系统找不到指定的文件

但是如果我复制命令并从命令行运行它,它工作正常.

我知道使用 JavaCompiler 类来编译我的文件,但我更愿意先解决这个问题,因为我无法从 Java 运行任何 dos 应用程序或 .bat 文件.我可以很好地运行像 notepad.exe 这样的 GUI 程序.

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, "即将使用以下命令运行 javac:");字符串 commandToOutput = "";对于(字符串命令:命令){commandToOutput += command + " ";}logger.log(Level.INFO, commandToOutput);ProcessBuilder processBuilder = new ProcessBuilder(commands);进程 p = processBuilder.start();

编辑 2

String[] commands = new String[]{"C:\"Program Files"\Java\jdk1.6.0_23\bin\javac.exe", "-d", """ + tempDir + """, "-classpath", 类路径};

编辑 3

为什么第二个命令数组有效,但第一个不在下面.

//这给了我 CreateProcess error=5, Access is denied命令 = 新字符串[]{"C:\"程序文件"\Java\jdk1.6.0_23\bin\javac.exe"};//这有效命令 = 新字符串[]{"C:\Windows\System32\cmd.exe",/C","C:\"程序文件"\Java\jdk1.6.0_23\bin\javac.exe",};

解决方案

您传递给 ProcessBuilder 的字符串数组应该为每个数组元素包含一个参数,而不是单个大字符串中的所有内容.

试试这个:

<前>字符串 [] 命令 = 新字符串 []{"C:\Windows\System32\cmd.exe",/C","C:\"程序文件"\Java\jdk1.6.0_23\bin\javac.exe"};

顺便说一句:不需要调用cmd.exe,直接把javac.exe传给ProcessBuilder

<前>ProcessBuilder builder = new ProcessBuilder("C:\"Program Files"\Java\jdk1.6.0_23\bin\javac.exe", "\Path\To\MyClass.java");

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:WindowsSystem32cmd.exe /c C:\"Program Files"Javajdk1.6.0_23injavac.exe" "C:\"Program Files"Javajdk1.6.0_23injavac.exe" and surrounding the entire path to javac with double quotes but still nothing.

I get the error

Cannot run program "C:WindowsSystem32cmd.exe /c C:"Program Files"Javajdk1.6.0_23injavac.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 p = 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"
);

这篇关于在 Windows 7 下无法使用 ProcessBuilder 在 Java 中执行 javac 或其他命令行应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆