Null Pointer Exception While Using Java Compiler API

Null Pointer Exception while using Java Compiler API

I suspect you're running into this problem - running the code with a JRE instead of a JDK.

When you run SimpleCompileTest, try explicitly specifying the version of java.exe you're using as the one in your JDK directory.

run() method of interface tool throws NullPointerException in JDK 9

Replace Hello.java with the full path to Hello.java file (ex C:\Test\Hello.java). Anyway, I didn't get NullPointerException even when the path to Hello.java is wrong.

UPDATE

I was able to reproduce your java.lang.NullPointerException error. It appears when running the program with JRE instead of JDK. This way, no compiler is found since the compiler is only available in JDK version.

UPDATE2

So you are using the command line to run the program.

When executing with %jdk_home%/bin/java you are using JDK and when executing with %jdk_home%/jre/bin/java you are using JRE.

I see you compile with JDK 9.0.1 and run with JRE 9.0.4. To make it work execute something like:

%JDK 9.0.1 home%/bin/java CallingJavaCompiler

getting null pointer execption when using ToolProvider.getSystemJavaCompiler() below is my code

finally I got the issue ,this is because of different version of Tools.jar
I was using different Tools.jar in my class path as I was using JDK 1.7 but using Tools.jar 1.6 version.
Now it is fixed !!!!

java 10 compilaton Null Pointer Exception

It is a reported unresolved bug. The bug is planned to be resolved in jdk 11.

JDK-8203195-Anonymous class type inference results in NPE

Type:                 Bug
Status: In Progress
Priority: P2
Resolution: Unresolved
Affects Version/s: 9, 10, 10.0.1, 11
Fix Version/s: 11
Component/s: tools
Labels: dcsfai reproducer-yes webbug

Subcomponent: javac
CPU: generic
OS: generic

https://bugs.openjdk.java.net/projects/JDK/issues/JDK-8203195?filter=allopenissues

However, there is a work around mentioned in the bug description which states:

Interestingly, changing A.java to do the following:

Object baz => foo.foo(new B<Object>() {});
or changing foo/B.java to the
following:

 package foo;

public class B<T> {

B(int baz) { }

protected B() { }
}

results in a successful compilation.



Related Topics



Leave a reply



Submit