Asked By
Sedra
270 points
N/A
Posted on - 05/13/2011
Hi,
Thank you for reading my post. I am making a simple IDE for C++. I have a compiler installed. I created a IDE like text editor where I can type my code and save it. Now I want a button which can compile the code on click and output the result. How do I do it? Is there anything as simple as system() in C++? I have googled for system() in java. But not much of a success.
Thanks again.
Compiling a C++ program from java
There are several ways to do so. But all firstly and simply need to set up a process. You can use the processbuilder or Process class to do so.
Then initialize the process with current runtime.
Pass the location of the exe file to it to execute.
It will be something like this:
Process p = Runtime.getRuntime().exec(" path to executable ");
Compiling a C++ program from java
Sedra,
Here is a article about using process inside java : Click Here
Hope these will help to learn the usage.
Answered By
Sedra
270 points
N/A
#92801
Compiling a C++ program from java
Thanks both of you very much.
I am using,
Process p = Runtime.getRuntime().exec("cmd /CÂ "C:\Program Files\CodeBlocks\MinGW\bin\mingw32-g++.exe" temp.cpp ");
I can see the file compile but when I go to the directory a new file named a.exe is created. But when I do some bad coding It doesn't show any error.
I need to track the error to verify whether the code is compiled correctly.
Compiling a C++ program from java
You will have to watch for the output of the process. First declare a bufferedReader to pipeline the error there.
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
Then just read it line by line, from the stdError to see the outputs.
Answered By
Sedra
270 points
N/A
#92803
Compiling a C++ program from java
Thanks a lot.
But I don't see it reading anything at all. Its just as it was before.
 public String compile()
      {
          String log="";
           try {
               String s= null;
           Process p = Runtime.getRuntime().exec("cmd /C "C:\Program Files\CodeBlocks\MinGW\bin\mingw32-g++.exe" temp.cpp ");
           BufferedReader stdError = new BufferedReader(new
                InputStreamReader(p.getErrorStream()));
           boolean error=false;
           while ((s = stdError.readLine()) != null) {
               //System.out.println(s);
               log+=rt.LineByLineTrans(s);
               error=true;
               log+="n";
           }
      Â
       } catch (IOException e) {
           e.printStackTrace();
       }
           return log;
      }
Compiling a C++ program from java
May be the code written in temp.cpp does not include any error.
The mingw32 compiler does not return anything on successful compilation.
If the temp.cpp is a perfect code the process won't return anything.
Try giving some code with a error or two.
Answered By
Sedra
270 points
N/A
#92805
Compiling a C++ program from java
Oh yeah!
You don't know how much pleased I am.
Thanks to all of you.