1

Am trying to run the source code from this link

Compile and run source code from Java application

I installed the Mingw32 compiler changed the compiler location path and getting this error when running a sample .cpp file in Eclipse.

public class C_Compile {
    public static void main(String args[]){

        String ret = compile();
        System.out.println(ret);

    }
        public static String compile()
        {
            String log="";
             try {
                 String s= null;
               //change this string to your compilers location
             Process p = Runtime.getRuntime().exec("cmd /C  \"C:\\MinGW\\bin\\mingw32-gcc-4.6.2.exe\" C:\\MinGW\\bin\\Hello.cpp ");

             BufferedReader stdError = new BufferedReader(new 
                  InputStreamReader(p.getErrorStream()));
             boolean error=false;

             log+="\n....\n";
             while ((s = stdError.readLine()) != null) {
                 log+=s;
                 error=true;
                 log+="\n";
             }
             if(error==false) log+="Compilation successful !!!";

         } catch (IOException e) {
             e.printStackTrace();
         }
             return log;
        }


      public int runProgram() 
        {
            int ret = -1;
           try
             {            
                 Runtime rt = Runtime.getRuntime();
                 Process proc = rt.exec("cmd.exe /c start a.exe");
                 proc.waitFor();
                 ret = proc.exitValue();
             } catch (Throwable t)
               {
                 t.printStackTrace();
                 return ret;
               }
           return ret;                      
        }}

Errors:

mingw32-gcc-4.6.2.exe: error: CreateProcess: No such file or directory

Can anyone tell me where to place my Source .cpp File. Thanks

2 Answers 2

1

The error message indicates, that the gcc compiler itself was not found. Why don't you use gcc.exe instead of mingw32-gcc-4.6.2.exe anyway? If you do an update of MinGW, the latter will get invalid! Also you do not need to use \" in the string, when the path does not contain whitespace characters.

You can place your cpp file then anywhere you want, providing the path to that gcc. Exec should also have a parameter dir, that you can set to the directory of your cpp.

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

5 Comments

if the gcc is on your PATH environment (typing gcc -v in a shell should print something like Target: mingw32 and the version) - you can also simply execute cmd /C gcc <PathToMy.cpp>.
I used gcc.exe still same error appearing: Process p = Runtime.getRuntime().exec("cmd /C \"C:\\MinGW\\bin\\gcc.exe\" Hello.cpp ");
The same error? Or a different error telling you, hello.cpp could not be found? As I said, you either have to provide the path to your Hello.cpp (it should not be located in the bin directory of your gcc! If so - place it in a project directory) - or you provide the directory with the exec-command as here: with Runtime.getRuntime().exec("cmd /C gcc hello.cpp", null, new java.io.File(myDirectory));
You can also add the bin-path of your MinGW installation to the Environment Variables "PATH". Should be easy to google how to do that. See also the Installation Instructions of MinGW (search for PATH)
added it to my environment variables..Now it says "compilaiton successful" but even if i delete the .cpp file, it still says compilation successful: strange
1
    public static void CompileCprog(String filename){

        File dir = new File("C://Users//JohnDoe//workspace//Project");

        try {  
            String exeName = filename.substring(0, filename.length() - 2);
            Process p = Runtime.getRuntime().exec("cmd /C gcc " + filename + " -o " + exeName, null, dir);  
//          Process p = Runtime.getRuntime().exec("cmd /C dir", null, dir);  
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
            String line = null;  
            while ((line = in.readLine()) != null) {  
                System.out.println(line);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }   
    }

This works perfectly for me.

The "dir" variable can be set to any location you want.

The first "p" process compiles a program and produces a .exe file with the same name (minus the .c) in the same location of the program you are compiling.

The buffered reader can be used if there is output from your command. If you changed the command string to .exec("cmd /C dir"); the result of this will be printed in the output. (im using eclipse)

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.