I need to compile and run source code (single file), written in Python, Pascal or C, from my Java application.
I will need to know:
In general, you'll want to launch a Process
that runs the external program (first the compiler, and then the compiled binary in your case), using either Runtime.getRuntime().exec()
or a ProcessBuilder
to spawn the Process
(since your compiler probably takes a complicated set of options, the ProcessBuilder
is likely a better option). This will allow you to grab the output from the process as it executes (so you can monitor the compiler output for warnings or errors), as well as its return code.
You may find the following examples helpful:
http://www.rgagnon.com/javadetails/java-0014.html
To get the return code of a running Process
, just use the waitFor()
method. This is convenient if you don't care about any of the output and just want the return value.