Reputation: 148
//Following is Hello.java file.
public class Hello{
public static void main(String... s){
System.out.println("hello world");
}
}
I'm trying to compile the above class using Java Compiler API as following:
import javax.tools.*;
public class CallingJavaCompiler{
public static void main(String... s){
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
int i = comp.run(System.in,System.out,System.err,"Hello.java");
System.out.println(i);
}
}
While executing CallingJavaCompiler class (using JDK 9), it throws java.lang.NullPointerException at run method. I'm unable to understand why it's throwing the exception when there's a string type argument?
Here is the stacktrace which I'm getting while executing it:
Exception in thread "main" java.lang.NullPointerException
at CallingJavaCompiler.main(CallingJavaCompiler.java:5)
On the other hand excecuting same program i.e, CallingJavaCompiler in JDK 8, it is executing without any runtime exception.
Please help me in understanding why is it throwing Null Pointer Exception (NPE) when I'm executing it using JDK 9?
Note: I'm compiling above piece of code using javac 9.0.4 and executing it with java version 9.0.4. While compiling and executing with these version, I'm getting NPE, but not with JDK 8.
Upvotes: 2
Views: 309
Reputation:
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
Upvotes: 4
Reputation: 29720
From the documentation of ToolProvider#getSystemJavaCompiler
:
This implementation returns the compiler provided by the jdk.compiler module if that module is available, and null otherwise.
I suspect that your jdk.compiler
module is unavailable for some reason (you might be running the program with JRE 9 and not JDK 9), hence why you'd be receiving a NullPointerException
. To be sure, I recommend adding it to your module-info.java
, even though it doesn't seem to be required.
module Hello {
requires jdk.compiler;
}
Anyway, I was able to solve your problem by supplying the full path to Hello.java
, as shown below. This works on Windows; if you use any other OS, then you should use File.separator
or System.getProperty("file.separator")
rather than \\
.
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
int i = comp.run(System.in, System.out, System.err,"src\\main\\java\\Hello.java");
System.out.println(i);
Output:
0
Note: The path begins from the root directory of my project (the parent of the source folder, src
). Remember to include any packages by appending to the directory where necessary.
Upvotes: 3