无法调用“gcc”从 Java 程序内部
我试图从 Java 程序调用 GNU C 编译器来编译 c++ 文件,但出现错误:
这是程序
class HelloWorld {
public static void main(String[] args) {
Runtime sys = Runtime.getRuntime();
System.out.println("Hello World!");
try {
String com = "g++ NB.cpp -o NNN";
System.out.println(com);
Process p = sys.exec(com);
}
catch (Exception ep) {
System.err.println(ep);}
}
}
这是编译并运行程序时得到的结果
$javac HelloWorld.java
$java HelloWorld
Hello World!
gcc NB.cpp -o NB
java.io.IOException: Cannot run program "g++": CreateProcess error=5, Access is denied
这是 gcc 所在的位置
$ which gcc
/usr/bin/gcc
这里如果PATH 的内容
$ echo $PATH
/usr/local/bin:/usr/bin:/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin:/cygdrive/c/Program Files (x86)/MiKTeX 2.9/miktex/bin:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windo
ws/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/d/SourceForge/vectorpascalcom: D:/Cygwin/bin:/cygdrive/d/make382:/cygdrive/d/usr/bin:/cygdrive/d/Program Files/TortoiseSV
N/bin:/cygdrive/d/SourceForge/vectorpascalcom:/usr/bin:/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin
有人可以帮忙吗?
I am trying to call the GNU C compiler from a Java program to compile c++ file, but I get an error:
Here is the program
class HelloWorld {
public static void main(String[] args) {
Runtime sys = Runtime.getRuntime();
System.out.println("Hello World!");
try {
String com = "g++ NB.cpp -o NNN";
System.out.println(com);
Process p = sys.exec(com);
}
catch (Exception ep) {
System.err.println(ep);}
}
}
Here is what I get when I compiler and run the program
$javac HelloWorld.java
$java HelloWorld
Hello World!
gcc NB.cpp -o NB
java.io.IOException: Cannot run program "g++": CreateProcess error=5, Access is denied
Here is where the gcc is resides
$ which gcc
/usr/bin/gcc
And here if the contents of the PATH
$ echo $PATH
/usr/local/bin:/usr/bin:/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin:/cygdrive/c/Program Files (x86)/MiKTeX 2.9/miktex/bin:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windo
ws/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/d/SourceForge/vectorpascalcom: D:/Cygwin/bin:/cygdrive/d/make382:/cygdrive/d/usr/bin:/cygdrive/d/Program Files/TortoiseSV
N/bin:/cygdrive/d/SourceForge/vectorpascalcom:/usr/bin:/cygdrive/c/Program Files/Java/jdk1.6.0_26/bin
Can any one help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Cygwin 中的
g++
通常是指向g++-3
或g++-4
的符号链接,但 Cygwin 符号链接对于非 Cygwin 程序并不透明。因此,您需要直接调用符号链接目标。g++
in Cygwin usually is a symlink to eitherg++-3
org++-4
, but Cygwin symlinks aren't transparent to non-Cygwin programs. Therefore you need to invoke the symlink target directly.确保您通过 cygwin 运行 java 应用程序,而不仅仅是普通的 Windows 命令 shell。另外,请尝试自行运行该命令以确保其正常工作。
最后,这可能不适用于您的问题,但您应该阅读著名的文章 当 Runtime.exec() 无论如何都不会时。
Make sure you're running the java application through cygwin and not just a normal windows command shell. Also, try running the command on your own to make sure it's working.
And finally, this might not be applicable to your issue, but you should read the famous article when Runtime.exec() won't anyway.
尝试用这个包裹它:
Try wrapping it with this:
我不认为
javac
和java
是 Cygwin 可执行文件(即,它们不使用cygwin1.dll
),所以它们不是将识别 Cygwin 特定的文件路径。您可以从 Cygwin shell 执行它们,但也可以对任何其他非 Cygwin Windows 可执行文件执行相同的操作。就您的 Java 进程而言,g++
不是/usr/bin/g++
,因为没有/usr/bin
目录。 (/usr/bin
实际上是 Cygwin 挂载点;对应的 Windows 目录是C:\cygwin\bin
。)试试这个:
I don't think
javac
andjava
are Cygwin executables (i.e., they don't usecygwin1.dll
), so they're not going to recognize Cygwin-specific file paths. You can execute them from a Cygwin shell, but you can do the same with any other non-Cygwin Windows executable. As far as your Java process is concerned,g++
isn't/usr/bin/g++
, because there is no/usr/bin
directory. (/usr/bin
is actually a Cygwin mount point; the corresponding Windows directory isC:\cygwin\bin
.)Try this: