Calling C compiler from java program

試著忘記壹切 提交于 2019-12-12 19:26:37

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/19627788/calling-c-compiler-from-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!