Null Pointer Exception while using Java Compiler API
Asked Answered
L

6

11

MyClass.java:

package test;
public class MyClass {
    public void myMethod(){
        System.out.println("My Method Called");
    }
}

Listing for SimpleCompileTest.java that compiles the MyClass.java file.

SimpleCompileTest.java:

package test;
import javax.tools.*;
public class SimpleCompileTest {
    public static void main(String[] args) {
String fileToCompile = "test" + java.io.File.separator +"MyClass.java";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, fileToCompile);
        if(compilationResult == 0){
            System.out.println("Compilation is successful");
        }else{
            System.out.println("Compilation Failed");
        }
    }
}

I am executing the SimpleCompileTest class and getting a NullPointerException. The ToolProvider.getSystemJavaCompiler() is returning null. Can someone tell me what is wrong with the code

Lath answered 30/3, 2010 at 7:20 Comment(2)
can you please edit your code with tags?Bastien
So your problem is that ToolProvider.getSystemJavaCompiler() returns null?Wilmot
O
16

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.

Obsess answered 30/3, 2010 at 7:30 Comment(4)
How do you explicitly specify the version of Java.exe you're using as the one in your JDK? I tried Run As but it already Project JRE selected as the JRESatisfied
@Imray: I'm not sure what you're asking - if that's the selected JRE, that's what will run. If you want a different one, select a different JRE. It sounds like you need to ask a new question with your exact problem.Obsess
In your answer you say: "When you run SimpleCompileTest, try explicitly specifying the version of java.exe you're using as the one in your JDK directory." I'm asking, how do you do that?Satisfied
@Imray: I'm saying that presumably the "Project JRE" you've got configured is simply not the right one. So edit that configuration.Obsess
B
18

I got the same error. Maybe I am too late to answer this question, but I share my own experiences, it might help someone else facing the same issue in the future. I was playing around with the source code at Compile Java Files At Runtime.

I was getting java.lang.NullPointerException as it is mentioned. I printed out the Java home directory with System.out.println(System.getProperty("java.home"));, and noticed my Eclipse was pointing to "C:\Program Files\Java\jre7" even after I changed my preferences to use JDK1.7 instead of JRE1.7.

I found a workaround by forcing the usage of JDK1.7 by setting system property like this:

System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02");

Then I compiled my program and did not get any NullPointerException.

Bradwell answered 8/6, 2012 at 7:40 Comment(2)
You are great dear. you have solved my problem of last 3 weeks. thanksLalalalage
I tried this to make compilation-toolkit work. I had to use System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02\jre"); to make it work.Albumen
O
16

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.

Obsess answered 30/3, 2010 at 7:30 Comment(4)
How do you explicitly specify the version of Java.exe you're using as the one in your JDK? I tried Run As but it already Project JRE selected as the JRESatisfied
@Imray: I'm not sure what you're asking - if that's the selected JRE, that's what will run. If you want a different one, select a different JRE. It sounds like you need to ask a new question with your exact problem.Obsess
In your answer you say: "When you run SimpleCompileTest, try explicitly specifying the version of java.exe you're using as the one in your JDK directory." I'm asking, how do you do that?Satisfied
@Imray: I'm saying that presumably the "Project JRE" you've got configured is simply not the right one. So edit that configuration.Obsess
P
1

Probably you have a JRE instead of JDK installed. https://bugs.java.com/bugdatabase/view_bug?bug_id=6477844

Pitsaw answered 30/3, 2010 at 7:30 Comment(0)
C
1

I was having the same problem

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

was returning null. Even after using

System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.8.0_31");

was returning null.

However the issue was simple as this code is in the Tools.jar found in Java\jdk1.8.0_31\lib if you are using JDK. what i did was to go to project-->properties-->Java Build Path-->order and export--> and moved tool.jar to the top of other JRE and projects. This helped me get rid of null hope this help you as well.Happy compiling...:-)

Counterspy answered 10/3, 2015 at 12:12 Comment(0)
V
0

Its working with Java application by expicitily including the tools.jar but for web application not working. Throwing Nullpointer

Valiant answered 29/2, 2012 at 10:30 Comment(0)
S
0

SUPPLEMENT: The answer above is to set the java.home system property within the program. That works for me too, but it's not a very general solution, since you've hard-coded for one jdk version. The alternative that I'm using now is to give the full path to "java" on the command line when I run the program. Such as (consistent with examples above):

C:\\Program Files\Java\jdk1.7.0_02\jre\bin\java -cp ..\classes path.to.program.myProgram

Giving the full path to the jdk version means that's the version that's running the program, so that's the one that will be fetched with ToolProvider.getSystemJavaCompiler();

Suzette answered 22/8, 2014 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.