Compile and Run Java Program from another Java Program [closed]

依然范特西╮ 提交于 2019-12-02 13:17:22

In my opinion better solution is use of the Java 6 Compiler API. You should look at javax.tools package documentation too.

You should use the Runtime class to do so.

code file --x.java to execute-y.java

Now you should be using the "exec" function of Runtime class.

like this

Process p=Runtime.getRuntime().exec("cmd /c javac y.java && java y.java");

so now the program executes from.a process made by this class.

This method returns a process

so in order to receive the output of this command just executed do like this

p.getOutputStream() // wrap this outputStream in a outputstream wtiter or may be a Printwriter and read it till its not null

and finally output it.

You can add command line arguments by adding another string in ProcessBuilder constructor like ProcessBuilder pb = new ProcessBuilder("java", clazz,"argument");

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