0

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.

4
  • 1
    Any reason you can't use a build tool like Maven or Ant?
    – earcam
    Aug 24, 2013 at 0:23
  • "So, am I on the right track?" If you mean 'on right track' by doing it in the command line first, I don't see the point. The way it is implemented in the Java compilation API is quite different in terms of finding all the required source files. From memory it requires implementing a JavaFileManager. Aug 24, 2013 at 0:26
  • @earcam I'm actually trying to learn the actual API, and not developing a project that requires me to do so per se.
    – Gx.Zero
    Aug 24, 2013 at 1:20
  • @AndrewThompson Err. The compile method has an argument of 'Iterable<String> options'. That's why I thought that successfully doing it in CLI means success in the API.
    – Gx.Zero
    Aug 24, 2013 at 1:23

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.