Compile and run source code from Java application

前端 未结 5 2117
渐次进展
渐次进展 2021-02-10 02:05

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:

  • if compile process was
5条回答
  •  臣服心动
    2021-02-10 02:41

    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.

提交回复
热议问题