Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set classpath for com.sun.tools.javac.Main.compile() function?

I am using com.sun.tools.javac.Main.compile() function to compile java file at run time from my struts projects. But for some files they need some specific jars like axis2. I have the jars but how can i set them to classpath to compile the java file at runtime? I have tried with System.setProperty("java.class.path","jar dir"); but failed to compile.

like image 633
Sweet Dream Avatar asked Sep 03 '12 10:09

Sweet Dream


1 Answers

The following code which 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 {
            //Specify classpath using next to -cp
            //This looks just like how we specify parameters for javac
            String[] optionsAndSources = {
                "-g", "-source", "1.5",
                "-target", "1.5", 
                "-cp", ".:/home/JavaCode/MyJavaCode.jar",
                "Apple.java"
            };
            PrintWriter out = new PrintWriter(new FileWriter("./out.txt"));
            int status =  Main.compile(optionsAndSources, out);
            System.out.println("status: " + status);
            System.out.println("complete: ");
        }catch (Exception e) {}
    } 
}

Note: To compile this AClass.java, tools.jar needs to be in the classpath, which is not there by default, so you will have to specify it.

If you are using Java 1.6 then you should consider using javax.tools.JavaCompiler instead, its getTask() methods takes an argument options which can have the classpath.

For example:

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>();
        // set compiler's classpath to be same as the runtime's
        options.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path")));
        //Add more options including classpath
        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        final JavaCompiler.CompilationTask task = compiler.getTask(/*default System.err*/ null,
            /*std file manager*/ null,
            /*std DiagnosticListener */  null,
            /*compiler options*/ options,
            /*no annotation*/  null,
            Arrays.asList(source));
       return task.call();
}

com.sun.tools.javac.Main is deprecated and undocumented too.

like image 102
Curious Avatar answered Nov 05 '22 03:11

Curious