Compiling a Java Project using Compiler API

253 views Asked by At

I'm trying to compile a whole java project dynamically using the Compiler API. My initial thoughts of achieving this is to first know how to compile it in one-line using command line, then apply the same principle/parameters on the compiler object. Problem is, I never did, err, compiled using CLI. (Disadvantage of using an IDE? Haha)

So, am I on the right track? Moreover, can the project compilation achieved in one line execution? I'm having a hard time figuring this out because of the fact that it's a project, it contains packages galore.

1

There are 1 answers

0
earcam On BEST ANSWER

So you either want to learn javac or Java Compiler API?

If you want CLI compilation look at javac (Linux | windows).

Alternatively for API, then programmatic use of Java's compiler API will definitely require more that one line, this will get you the compiler:

JavaCompilercompiler =ToolProvider.getSystemJavaCompiler();

Then you'll still need to load classes, write out byte code, and possibly package as a JAR.

You should use StandardJavaFileManager as you've probably many classes to manage, there's an example in the top of the JavaCompiler javadoc, but search for StandardJavaFileManager+JavaCompiler+example to find clearer examples like this blog post.