Java SE 6 Compiler API

Now that Java SE is released I wanted to cover one of my favorite feature, the Compiler API. I already wrote about a cool hack that can be done in previous versions of the JDK to compile Java code at runtime. I also wrote about compiling Groovy at runtime. Now I will cover how to compile Java code using the new Compiler API introduced in the JDK SE 6. Please not that the following code use classes that are currently only available with the JDK, not the JRE.

To get started you will need to instantiate a Java Compiler and Java File Manger.

JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sjfm = jc.getStandardFileManager(null, null, null);

The next step is to create a compilation task to the Java Compiler. You can add as many task to the Java compiler as you may need. Also note that the getJavaFileObjects method accepts a variable number of arguments so you could pass in as many files as you may need. Also, don’t forget to close the Java File Manager when you are done with it.

File javaFile = new File(”c:/src/com/juixe/Entity.java”);
// getJavaFileObjects’ param is a vararg
Iterable fileObjects = sjfm.getJavaFileObjects(javaFile);
jc.getTask(null, sjfm, null, null, null, fileObjects).call();
// Add more compilation tasks
sjfm.close();

At this point with the above code, if there are no errors the class files would compile to c:/src. If you want to output the class files in a different location you can add options to the compilation task with code similar to the following.

String[] options = new String[]{”-d”, “c:/bin”};
jc.getTask(null, sjfm, null, Arrays.asList(options), null, fileObjects).call();

Once you have compiled the Java source file using the new Java Compiler API, you may want to load the class into your running system. To load a Java class compiled using the Compiler API you can use code similar to that used to load a class compiled using embedded Groovy. Here is how you can load a newly compiled class at runtime.

File outputDir = new File(”c:/bin”);
URL[] urls = new URL[]{outputDir.toURL()};
URLClassLoader ucl = new URLClassLoader(urls, cl);
Class clazz = ucl.loadClass(”com.juixe.Entity”);

Technorati Tags: , , , , , , , , ,

Enjoy. Share. Be Happy.
  • Twitter
  • Facebook
  • StumbleUpon
  • del.icio.us
  • Tumblr
  • Google Bookmarks
  • FriendFeed
  • Yahoo! Buzz
  • Reddit
  • Digg
  • HackerNews
  • Suggest to Techmeme via Twitter
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • Mixx
  • Furl

Related posts:

  1. Invoke Javac At Runtime
  2. Embedding Groovy
  3. Java SE 6 Released
  4. The Java 3D API and Java Binding for OpenGL
  5. Visual Basic And The Java Platform

This entry was posted in Java, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Tanmay
    Posted August 23, 2007 at 9:40 am | Permalink

    Great post!!

    I’m trying to get it working but I’m getting NullPointerException when I try to get a handle to the Java compiler.

    JavaCompiler jc = ToolProvider.getSystemJavaCompiler();

    Any ideas why that might be happening?

    Thanks

  2. Tanmay
    Posted August 23, 2007 at 9:46 am | Permalink

    Never mind, I figured it out. Wrong Java 6 inside eclipse. :)

  3. Graham Kirby
    Posted November 30, 2007 at 5:21 am | Permalink

    [Advert warning - I am the author]

    People may also be interested in the wrapper code available at:

    http://www-systems.cs.st-andrews.ac.uk/wiki/Dynamic_Java_Compiler

    Its aim is the same as the new facilities in Java 6, but has been around for much longer. I think the API is a bit simpler, but you can make up your own mind on that…

One Trackback

  1. By programatical compiltion - Java Forums on November 10, 2007 at 12:28 pm

    [...] Check the following resources: The Java Compiler API Juixe TechKnow ? Java SE 6 Compiler API [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*