Compiler API and Tomcat

Hi!

I am using the new compiler api to compile dynamically generated classes.

To get the compiler tool i call:

JavaCompiler compilerTool = ToolProvider.getSystemJavaCompiler();

when I run this from eclipse with the integrated tomcat everyhing works fine but if I deploy the application on the external tomcat i get a null pointer exception because getSystemJavaCompiler(); always returns null.

Somebody got an idea why?

[457 byte] By [_-Mithrandir-_a] at [2007-11-14]
# 1
Perhaps the external tomcat is running on JRE or does not allow ToolProvider to create a class loader for loading tools.jar. In JDK 7 I have added some logging facilities to help debug such situations. However, they are not yet available for JDK 6.
PeterAhea at 2007-7-8 > top of java,Developer Tools,Java Compiler...
# 2
I thought copying the tools.jar to the tomcat common/lib should work but I had to copy the jar to the jre/lib/ext folder to get this working.Thanks for the good work Peter, the API is really helpful.
_-Mithrandir-_a at 2007-7-8 > top of java,Developer Tools,Java Compiler...
# 3

sigh, NEVER put ANYTHING in the jre/lib/ext directory.

It's a bad thing that it's even there, as it causes systems to become mutually incompatible and to behave unpredictably when different versions of classes are needed/present.

The compiler API (and others) that depend on the local filesystem are disallowed by application servers for security reasons.

You've just given access to every cracker in the world to your system by allowing them to compile and run code on your machine with root privileges. Your boss will thank you.

jwentinga at 2007-7-8 > top of java,Developer Tools,Java Compiler...
# 4
ok you磛e told me how it shouldnt be donewhats your suggestion to solve the problem without violating security?btw my application is an internal one
_-Mithrandir-_a at 2007-7-8 > top of java,Developer Tools,Java Compiler...
# 5
don't do it.
jwentinga at 2007-7-8 > top of java,Developer Tools,Java Compiler...
# 6
smart solution! thanksI will award you the duke dollars SCNR
_-Mithrandir-_a at 2007-7-8 > top of java,Developer Tools,Java Compiler...
# 7
The problem is where the javax.tools.ToolProvider looks for the compiler. Typically, it won't be found the context class loader, so it creates a URLClassLoader that looks in
System.getProperty("java.home")/lib and
System.getProperty("java.home")/tools.jar
Tomcat does not set the java.home property correctly. So, you must set this manually in the Tomcat startup scripts.
Twisted at 2008-2-6 > top of java,Developer Tools,Java Compiler...