简体   繁体   English

Runtime.exec 不会运行 javac,但命令行会

[英]Runtime.exec won't run javac, but command line will

In my program, I create a.java file and then compile it.在我的程序中,我创建了一个.java 文件,然后编译它。 The command I use looks like this:我使用的命令如下所示:

javac -classpath [path to main directory]\my.jar -d [path to main directory]\bin [path to main directory]\src\pkg1\MyClass.java

This is called with the following code:这是使用以下代码调用的:

Runtime runtime = Runtime.getRuntime()
runtime.exec(command)

However, when I run it by clicking "Run" in Eclipse, I get the following error:但是,当我通过单击 Eclipse 中的“运行”来运行它时,出现以下错误:

java.io.IOException: Cannot run program "javac": CreateProcess error=2, 
    The system cannot find the file specified

Now, I know what this means: I haven't installed Java correctly, and my path variable needs to include the JDK.现在,我知道这意味着什么:我没有正确安装 Java,我的路径变量需要包含 JDK。 However, the problem with that simple hypothesis is that it works perfectly fine when I use the exact same command from the command line (obviously not with runtime.exec() , but still).然而,这个简单假设的问题在于,当我从命令行使用完全相同的命令时它工作得非常好(显然不是runtime.exec() ,但仍然如此)。 I know it's not a fault with my Java installation or with the command itself, because otherwise it wouldn't work from the command line, so what's left and how do I fix it?我知道这不是我的 Java 安装或命令本身的故障,因为否则它将无法从命令行运行,那么剩下什么以及如何修复它?

The problem问题

You are asking the JVM to run a command named "javac", but it fails with an error:您要求 JVM 运行名为“javac”的命令,但它失败并出现错误:

java.io.IOException: Cannot run program "javac": CreateProcess error=2,
The system cannot find the file specified

Explanation解释

The JVM does not know where to find "javac". JVM 不知道在哪里可以找到“javac”。 Whether or not you are able to run "javac" yourself is separate from whether the JVM can also run that command.是否能够自己运行“javac”与JVM是否也可以运行该命令是分开的。

Reproducing the behavior重现行为

Here's a simple program to reproduce the behavior – it tries to run "test.sh":这是一个重现该行为的简单程序——它尝试运行“test.sh”:

String command = "test.sh";
Process process = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println("command: [" + command + "] -- " + "output: [" + br.readLine() + "]");

Running that code will throw an exception because the JVM doesn't know where to find "test.sh":运行该代码将引发异常,因为 JVM 不知道在哪里可以找到“test.sh”:

java.io.IOException: Cannot run program "test.sh": error=2, No such file or directory

Solution解决方案

I do in fact have "test.sh" located on my computer, but I didn't specify the full path earlier ("/var/tmp") in the Java program.事实上,我的计算机上确实有“test.sh”,但我之前没有在 Java 程序中指定完整路径(“/var/tmp”)。 The script is pretty simple, it just runs the "date" command:该脚本非常简单,它只运行“日期”命令:

% cat /var/tmp/test.sh
#!/bin/sh
date "+%Y-%m-%d"

% /var/tmp/test.sh 
2022-08-05

Editing the command so that it's using an absolute path to "test.sh" works fine:编辑命令使其使用“test.sh”的绝对路径可以正常工作:

String command = "/var/tmp/test.sh";

command: [/var/tmp/test.sh] -- output: [2022-08-05]

Example: same for javac示例: javac相同

This works fine for "javac", too – here's the output from command "/usr/bin/javac -version" :这也适用于“javac”——这是来自命令"/usr/bin/javac -version"的 output:

command: [/usr/bin/javac -version] -- output: [javac 18.0.1.1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM