The following code that uses com.sun.tools.javac.Main worked for me:
Apple.java
//This class is packaged in a jar named MyJavaCode.jar import com.xyz.pqr.SomeJavaExamples; public class Apple { public static void main(String[] args) { System.out.println("hello from Apple.main()"); } }
AClass.java
import com.sun.tools.javac.Main; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class AClass { public static void main(String[] args) { try {
Note. To compile this AClass.java , tools.jar must be in the classpath , which is missing by default, so you will need to specify it.
If you are using Java 1.6 , you should use javax.tools.JavaCompiler instead, its getTask ( ) methods accept an options argument that may have a classpath .
For instance:
import javax.tools.JavaCompiler; import javax.tools.ToolProvider; import javax.tools.JavaFileObject; public final class AClass { private static boolean compile(JavaFileObject... source ){ List<String> options = new ArrayList<String>();
com.sun.tools.javac.Main deprecated and not documented.
source share