2

I have written a java program that compiles and executes C,c++,java programs ..I firstly tested it for java and it worked absolutely fine. Then I tested it for C but it gave errors.Please tell what I need to do..Here is the module which compiles the code..:

public void compileCode(String path,String lang)throws IOException
    {
        String cmd="";
        if(lang.equals("c")||lang.equals("cpp"))
            cmd="g++ Main"+threadNum+"."+lang+" -o "+threadNum;
        else if(lang.equals("java"))
            cmd="javac Main"+threadNum+".java";

        Process p=Runtime.getRuntime().exec(cmd,null,new File(path));

         String s=null;
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while ((s = stdError.readLine()) != null) {
            msg+=s+"\n";
            res=0;
        }
        if(res!=0)
            processCode(path,lang);
    }

And the error is :

Exception in thread "main" java.io.IOException: Cannot run program "g++" (in directory "C:\wamp\www\usercodes\lokesh"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at java.lang.Runtime.exec(Runtime.java:615)
    at java.lang.Runtime.exec(Runtime.java:448)
    at Contest.compileCode(Main.java:164)
    at Contest.makeFile(Main.java:154)
    at Contest.main(Main.java:52)
    at Main.main(Main.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:1

20)
Wayne Rooney
  • 1,587
  • 3
  • 17
  • 23

2 Answers2

2

For "g++" to work, there has to be a g++.exe on the PATH for windows. If it is a "g++.bat" or "g++.cmd", you have to call Runtime.exec with the exact name.

Arne
  • 2,106
  • 12
  • 9
  • No..I have a g++.exe on the path ..But its still dosent work..But if any other command(not necessay) g++ can do the trick u can tell me that also – Wayne Rooney Jun 29 '12 at 21:32
  • Runtime.exec appends ".exe" to find and start an executable that was given without extension. But Runtime.exec does not try the cmd or bat extension. Are you shure there is a g++.exe? – Arne Jun 30 '12 at 05:12
  • Yes I am sure..If u dont believe u can see my screen[link](http://img72.imageshack.us/img72/2250/image001cho.jpg) – Wayne Rooney Jun 30 '12 at 09:55
  • fine ;-). Do you have Sources for your jdk 7? Then have a look at ProcessBuilder Line 1029. That's where your exception comes from. – Arne Jun 30 '12 at 10:24
  • here it is:[link]http://hg.openjdk.java.net/jdk7/jdk7-gate/jdk/file/e947a98ea3c1/src/share/classes/java/lang/ProcessBuilder.java But I think there is a problem with the C compiler because java programs are running fine – Wayne Rooney Jun 30 '12 at 11:21
  • So your IOException comes from native code. whhat about using sysinternals.com process monitor to find out where the jre is looking for g++ and why it's not finding it? – Arne Jun 30 '12 at 12:36
  • I downloaded and struggled with it for an hour but could not understand what needs to be done..Can u clarify what I have to do in it.. – Wayne Rooney Jun 30 '12 at 16:27
  • If it helps u to fix the problem I have posted another thread relating to this [link](http://stackoverflow.com/q/11276421/1461963) – Wayne Rooney Jun 30 '12 at 18:36
0

I used process Builder instead and it worked out..Anyways thnx for ur time :) Here's the code...

public void compileCode(String path,String lang)throws IOException,InterruptedException
    {
        String cmd="";
        if(lang.equals("c")||lang.equals("cpp"))
            cmd="g++ "+path+"Main"+threadNum+"."+lang+" -o "+threadNum;
        else if(lang.equals("java"))
            cmd="javac Main"+threadNum+".java";
        ProcessBuilder process=new ProcessBuilder();
        process.directory(new File(path));
        process.command(new String[]{"cmd","/c",cmd});
        Process p=process.start();
        String s=null;
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while ((s = stdError.readLine()) != null) {
            msg+=s+"\n";
            res=0;
        }
        if(res!=0)
            processCode(path,lang);
    }
Wayne Rooney
  • 1,587
  • 3
  • 17
  • 23
  • in this version cmd.exe finds and starts g++. just to point out the difference. – Arne Jun 30 '12 at 05:06
  • But unfortunately I have a problem here.. In the next module where I run the code , the .exe file does not destroy when I use this: `ProcessBuilder process=new ProcessBuilder();process.directory(new File(path));Process p=process.start(); p.destroy();` – Wayne Rooney Jun 30 '12 at 09:56
  • Ok ..I found out that this process is not destrying because the process is actually cmd and that is creating a subprocess which I am not destroying.. – Wayne Rooney Jun 30 '12 at 11:08