|
Java™ by example!
|
|
|
How do I dynamically compile a Java source file and execute a method on it?
You can use the package sun.tools.javac located in JDK/lib/tools.jar. Make sure you add this Jar file to your classpath! When your invoke compile, you pass a String array containing the file to compile, and optionally also other compile options, eg. new String[] { "-classpath", "c:\mylibs", "Test.java" }. Main.java:
Test.java:
Running Main outputs:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|