5

So, java has a built in library dedicated for compiling java source code into .class files, and it is in javax.tools. So, I was wondering how exactly you get it to work. I've read through the javadoc, and it gives some examples in there, but when I use those examples, I get errors.

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);

That is the example oracle gives in order to get an instance of the StandardJavaFileManager class from which you can do much more. However, I'm having some issues with the very first line of that code. When I attempt to do ToolProvider.getSystemJavaCompiler();, it always returns null. In the javadocs for that method, it says, "returns the compiler provided with this platform or null if no compiler is provided." But they never show any other way of getting an instance of a JavaCompiler. I've tried many other ways, such as using a ServiceLoader to find any reference of it that I could, but to no prevail. How might I go about getting this to work?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Steven
  • 1,709
  • 3
  • 17
  • 27
  • http://www.java.net/node/688208 http://stackoverflow.com/questions/2543439/null-pointer-exception-while-using-java-compiler-api – nhahtdh Dec 04 '12 at 06:58

2 Answers2

9

Chances are you're running Java from a JRE directory instead of a JDK directory - you need to run a version which "knows" where the Java compiler is.

So for example, on my Windows boxing, running a tiny test app like this:

import javax.tools.*;

class Test {
    public static void main(String[] args) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        System.out.println(compiler);
    }
}

The results are:

c:\Users\Jon\Test>"\Program Files\Java\jdk1.7.0_09"\bin\java Test
com.sun.tools.javac.api.JavacTool@1e0f2f6

c:\Users\Jon\Test>"\Program Files\Java\jre7\bin\java" Test
null

As you can see, it's fine when I specifically run the JDK version, but I get null when running the JRE version. Check how you're starting Java.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Alright, so, using Eclipse, how do I change it to use my JDK rather than my JRE? – Steven Dec 04 '12 at 07:06
  • @StevenFontaine: Have a look in your project properties - that will show which environment you're building and running against. (You may well be able to change it separately for the run configuration, but you probably just want to change the one for the project... or even your workspace default.) – Jon Skeet Dec 04 '12 at 07:08
2

The compilation is often performed by directly invoking the javac compiler, which requires an installed Java Development Kit (JDK) or by calling com.sun.tools.javac.Main, which can be found in Sun's tools.jar. Sun's licensing allows tools.jar to be redistributed with the full Java Runtime Environment (JRE). Other ways to implement such dynamic capabilities include using an existing dynamic scripting language (such as JavaScript or Groovy) that integrates with the application's implementation language (see Resources) or writing a domain-specific language and associated language interpreter or compiler.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120