3

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?

7
  • It is most likely a permissions issue. The idea should work, but most likely your JVM doesn't have the right to invoke g++. Try running another executable and watching the output. Commented Aug 20, 2011 at 14:49
  • try to use absolute path for g++, instead of rely on PATH Commented Aug 20, 2011 at 14:55
  • I tried the absolute path, but the problem still exist! Commented Aug 20, 2011 at 16:16
  • I tried running another executable file, which already display some results, through the same program, but I did not run at all. Commented Aug 20, 2011 at 16:20
  • @You: Have a look at my deleted answer. A moderator thought it doesn't answer the question, well maybe it doesn't, but surely it does more than the other two answers. In short: this is no Java problem, make g++ run from the command prompt and it'll run from Java too. Commented Aug 21, 2011 at 2:12

4 Answers 4

4
+50

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That's it - at least on my computer. Here the target is /etc/alternatives/g++, without any extension, so it probably can't be executed on Windoze anyway. Making a copy called g++.exe brought me further (to another problem which is probably specific to XP 64).
Ah yes, I forgot about that; /etc/alternatives/g++ is another symlink. Making a copy does work of course, but the drawback is that things might get inconsistent when there's an update.
You're right, making a copy is wrong. The symlink leads to /usr/bin/g++-4.exe, but I can see no Windows directory corresponding to/usr/bin. Finally I've found C:\Programs\cygwin\bin\g++-4.exe, which complains when no input files are given, but otherwise does nothing at all. Strange.
1

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.

3 Comments

Well, I am running it through Cygwin. The commend “gcc NP.cpp –o NN” on its own from Cygwin works fine, but not from a Java program.
Have you tried the command that your program is running? G++ not gcc.
Yes, but I get the same problem
0

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);}
        } 
    }
}

1 Comment

This won't help, since the problem is running cygwin program from Windows.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.