2

I have a project for which I need to compile C programs. The project is in Java. I have to take the program into a JTextArea,(I am using Swing for GUI) , run it against a testfile and output the results into another JTextArea. But something seems to be wrong in my code.

Process p = new ProcessBuilder("c:\\MinGW\\bin\\cc.exe", "program.c").start();

program.c contains the program that user enter into TextArea, and "c:\MinGW\bin\cc.exe" is my c compiler. I checked that this is not raising any exception. It should create a file named a.exe in my current directory having program.c file, but it is not.What is the proper way to do it?

I saw two other post similar to this, but they also don't provide a simple clean solution for this. Also, I have to run the created executable file after it.

Thanks in advance.

4
  • ProcessBuilder won't raise an exception if something goes wrong within the process (ie, the compiler fails), you should be reading the output of the process to be sure that nothing has gone wrong... Commented Oct 28, 2013 at 5:38
  • 2
    Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec and (continue to) build the Process using a ProcessBuilder. "I saw two other post similar to this.." Links? Commented Oct 28, 2013 at 5:40
  • @AndrewThompson Those links I forgot. They were coming in suggestions when I was giving title to this post.I will work on this tonight. Commented Oct 29, 2013 at 5:04
  • stackoverflow.com/questions/12340922/… one was this @AndrewThompson This is my first time at any forum and also I am newbie to java. Commented Oct 29, 2013 at 5:13

1 Answer 1

2

What is the proper way to do it?

One way is to use javax.tools.JavaCompiler "to invoke Java™ programming language compilers from programs." There's an example here.

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

1 Comment

Sorry, I forgot that I had to accept answers which I found useful. :)

Your Answer

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