无法调用“gcc”从 Java 程序内部

发布于 2024-11-30 08:18:51 字数 1245 浏览 1 评论 0原文

我试图从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坠似风落 2024-12-07 08:18:51

Cygwin 中的 g++ 通常是指向 g++-3g++-4 的符号链接,但 Cygwin 符号链接对于非 Cygwin 程序并不透明。因此,您需要直接调用符号链接目标。

g++ in Cygwin usually is a symlink to either g++-3 or g++-4, but Cygwin symlinks aren't transparent to non-Cygwin programs. Therefore you need to invoke the symlink target directly.

梦魇绽荼蘼 2024-12-07 08:18:51

确保您通过 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.

只是在用心讲痛 2024-12-07 08:18:51

尝试用这个包裹它:

java.security.AccessController.doPriveleged(new java.security.PrivilegedAction() {
    public Object Run() {
        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);}
        } 
    }
}

Try wrapping it with this:

java.security.AccessController.doPriveleged(new java.security.PrivilegedAction() {
    public Object Run() {
        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);}
        } 
    }
}
别挽留 2024-12-07 08:18:51

我不认为 javacjava 是 Cygwin 可执行文件(即,它们不使用 cygwin1.dll),所以它们不是将识别 Cygwin 特定的文件路径。您可以从 Cygwin shell 执行它们,但也可以对任何其他非 Cygwin Windows 可执行文件执行相同的操作。就您的 Java 进程而言,g++ 不是 /usr/bin/g++,因为没有 /usr/bin 目录。 (/usr/bin 实际上是 Cygwin 挂载点;对应的 Windows 目录是 C:\cygwin\bin。)

试试这个:

 String com = "C:\\cygwin\\bin\\sh -c 'g++ NB.cpp -o NNN'";
 System.out.println(com);
 Process p = sys.exec(com);

I don't think javac and java are Cygwin executables (i.e., they don't use cygwin1.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 is C:\cygwin\bin.)

Try this:

 String com = "C:\\cygwin\\bin\\sh -c 'g++ NB.cpp -o NNN'";
 System.out.println(com);
 Process p = sys.exec(com);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文