Java: getSystemJavaCompiler returns null!

After reinstalling my laptop with Ubuntu Natty, I had an issue with one of my custom built tools.  I have this tool that installs a Tomcat-based web application and server.  In the part that compiles the JSON library, I received a NullPointerException.

On further examination, I found that ToolProvider.getSystemJavaCompiler() was returning null for some reason.  I found the answer on IBM DeveloperWorks:

The ToolProvider.getSystemJavaCompiler()method can return null if tools.jar is not in the application’s classpath. The CharStringCompilerclass detects this possible configuration problem and throws an exception with a recommendation for fixing the problem. Note that Sun’s licensing allows tools.jar to be redistributed with the JRE.

What I found was that I was using a JRE instead of a JDK installation.  Of course, when I switch the Java Home to my JDK, I had some other error and the App wouldn’t even run.  So, I added the tools.jar from the JDK installation to the classpath and ran it with the JRE, and everything worked fine.

So, here is the code I use to compile (including the catch for the missing tools.jar):

		JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
		// handle issue where tools.jar is not on the classpath
		if(jc == null) {
			log.logException(new InstallException("Could not access the system Java compiler -- check to make sure tools.jar is on the class path."));
		StandardJavaFileManager fm = jc.getStandardFileManager(null, null, null);
		File compilePath = new File(unzipPath, "org/json");
		Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(compilePath.listFiles());
		for(JavaFileObject f : files) {
			log.logInformation("Compiling - " + f.getName());
		CompilationTask task = jc.getTask(log.getStream(), fm, null, null, null, files);
		try {
		} catch (IOException e) {
			log.logException(new InstallException("Could not unzip " + zipPath, e));
		boolean result =;
		log.logInformation("**Compile result = " + result);


IBM DeveloperWorks: Create dynamic applications with

Leave a Comment

Your email address will not be published. Required fields are marked *