Take the 2-minute tour ×


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

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


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");
            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

share|improve this question
can you please edit your code with tags? –  bdhar Mar 30 '10 at 7:25
So your problem is that ToolProvider.getSystemJavaCompiler() returns null? –  Gabe Mar 30 '10 at 7:30

6 Answers 6

up vote 7 down vote accepted

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.

share|improve this answer
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 JRE –  Imray Jun 16 at 16:09
@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. –  Jon Skeet Jun 16 at 16:39
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? –  Imray Jun 16 at 17:15
@Imray: I'm saying that presumably the "Project JRE" you've got configured is simply not the right one. So edit that configuration. –  Jon Skeet Jun 16 at 17:17

I got same error. May be I am too late to answer this question. Posting here so it might help someone else facing same issue in future. I was planing around with source at Compile Java Files At Runtime

I was getting java.lang.NullPointerException as it is mentioned. I did "System.out.println(System.getProperty("java.home"));" and noticed my eclipse was pointing "C:\Program Files\Java\jre7" even after I changed my preferences to use JDK1.7 in place of JRE1.7.

I found a work around by forcing to use 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.

share|improve this answer
You are great dear. you have solved my problem of last 3 weeks. thanks –  adeel iqbal Feb 20 '13 at 13:24
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. –  Sebastian Nov 22 '13 at 21:01
This worked for me too! Thanks Rashid! –  Imray Jun 16 at 17:12

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

share|improve this answer

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

share|improve this answer

If including tools.jar doesn't work for web application, what would be the solution for that?

share|improve this answer

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();

share|improve this answer

Your Answer


