I know this has been asked and answered a lot but I still don't have a good solution and I still don't understand some parts. So I have the requirement to compile *.java files programmatically.

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

is what I'm using and (as expected) the compiler is null. Now, I do know that I have to use the JDK and not the JRE as a "runtime", but here is something I don't understand: isn't it enough to just put tools.jar in the classpath of the application and then have access to the Java Compiler API? If this is true, is there (I think there is) difference between a stand-alone java application and web-based application. Actually, I am trying to summon the JavaCompiler from a PlayFramework webapp so I figured out, maybe this solutions (with including the tools.jar) only works for stand-alone applications?

I also tried to create a custom ClassLoader and invoke the above mentioned method with reflection but all I am getting is null for the compiler object:

ClassLoader classloader = app.classloader();
File file = new File("lib/tools.jar");          
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
ClassLoader newCL = new URLClassLoader(urls, classloader) {
};
Class<?> loadClass = newCL.loadClass("javax.tools.ToolProvider");
Method method = loadClass.getMethod("getSystemJavaCompiler", null);             
Object object = method.invoke(null);
System.out.println("Object: " + object); // NULL

Explanation for the code above:

  • I haven't included try/catches for the sake of simplicity.
  • app.classloader() is a Play-method which returns the ClassLoader of the App
  • tools.jar is included in my lib folder of the Play project (this implies that it is on the project's classpath - according to the Play documentation)

I am pret

Edit
Report