Compile another java file from java file
Just out of curiosity. Can we compile & run a java file from another java program? If so, can you send a reference to that 开发者_C百科knowledge source?
Take a look at Java Compiler Api and this little example.
If you have the java source code already in a file, then you can just call the java compiler. The java compiler is built-in to the JVM libraries as of version 1.6.
The interface is documented here.
Didn't read it thoroughly, but maybe this helps.
If you're using Java 6, the best way to do this is through the javax.tools.JavaCompiler
interface.
If you're using an older version of Java, you must call javac
directly using Runtime.exec()
, then load the class data by subclassing ClassLoader
and overriding findClass
.
Yes, you can, but you need java compiler and not only java runtime. First you generate your source, save it and then use Dynamic class loading(tutorial http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html)
You can use javac (JDK is needed):
http://www.javaworld.com/javatips/jw-javatip131.html
You can do it by calling the cmd prompt or use the Main class from the Java Code.
I don't know remember well how is it, but I did it a long time ago.
精彩评论