Compiling a C++ program from java

Asked By 270 points N/A Posted on -
qa-featured

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.

SHARE
Best Answer by softwaresynthesis
Answered By 0 points N/A #92799

Compiling a C++ program from java

qa-featured

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

Answered By 0 points N/A #92800

Compiling a C++ program from java

qa-featured

Sedra,

Here is a article about using process inside java : Click Here

Hope these will help to learn the usage.

Answered By 270 points N/A #92801

Compiling a C++ program from java

qa-featured

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.

Best Answer
Best Answer
Answered By 0 points N/A #92802

Compiling a C++ program from java

qa-featured

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 270 points N/A #92803

Compiling a C++ program from java

qa-featured

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

Answered By 0 points N/A #92804

Compiling a C++ program from java

qa-featured

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 270 points N/A #92805

Compiling a C++ program from java

qa-featured

Oh yeah!

You don't know how much pleased I am.

Thanks to all of you.

Answered By 0 points N/A #92806

Compiling a C++ program from java

qa-featured

You are most welcome.

Related Questions