Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set classpath when I use javax.tools.JavaCompiler compile the source?

I use the class javax.tools.JavaCompiler (jdk6) to compile a source file, but the source file depends on some jar file. How to set the classpath of the javax.tools.JavaCompiler?

like image 411
Diablo.Wu Avatar asked Oct 14 '09 02:10

Diablo.Wu


People also ask

How to specify a classpath for Java compiler?

Use the -classpath option or CLASSPATH environment variable to do this. The class path is a sequence of directories (or zip files) which javac searches for classes not already defined in any of the files specified directly as command arguments.

Which command that we us to compile Java program to an executable bytecode?

The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.

Is javac the compiler?

The Java programming language compiler, javac , reads source files written in the Java programming language, and compiles them into bytecode class files.

What is the Java compiler called?

In fact, the Java compiler is often called the JVM compiler (for Java Virtual Machine). Consequently, you can write a Java program (on any platform) and use the JVM compiler (called javac) to generate a bytecode file (bytecode files use the extension . class).


2 Answers

The javax.tools.JavaCompiler#getTask() method takes an options parameter that allows to set compiler options. The following message describes an easy way to set them in order to access the calling program's classpath:

You need to configure the standard java file manager to know about the jar files(s) - you use the compiler options argument to do that.

By default the java compiler object only seems to know about the default locations for bootclasspath, extdirs and endorseddirs directories in terms of its classpath.

You need to add the calling program's current classpath to the java compiler instance's which gets passed on the the standard file manager, which will then find classes in the jar files.

Here's how I do it in the compiler wrapper I wrote

List<String> optionList = new ArrayList<String>(); // set compiler's classpath to be same as the runtime's optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path")));  // any other options you want optionList.addAll(Arrays.asList(options));  JavaCompiler.CompilationTask task = compiler.getTask(out,jfm,diagnostics,optionList,null,jfos); 

All you'll need then is to get the proper classpath set when running the calling program.

like image 93
Pascal Thivent Avatar answered Sep 22 '22 10:09

Pascal Thivent


The same problem occurred to me recently, finally I found two workarounds. You can set the class path either by invoke StandardJavaFileManager.setLocation(StandardLocation.CLASS_PATH, "YOUR_CLASS_PATH") or Compiler.getTask(ARG_0, ARG_1, ARG_2, CLASS_PATH_OPTIONS, just as the first answer posted here says.

like image 45
Wenlin.Wu Avatar answered Sep 26 '22 10:09

Wenlin.Wu