1

I found and modified the code below to programmatically compile a Java class:

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class CompileHello {

public static void main(String[] args)  {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    int result = compiler.run(null, null , null , "C:\\Users\\quickCoder\\Desktop\\Hello.java");

    System.out.println("Compile result code = " + result);
    }
}

However, I keep getting the following error:

Exception in thread "main" java.lang.NullPointerException
at CompileHello.main(CompileHello.java:8)

Line 8 is the following line:

  int result = compiler.run(null, null , null , "C:\\Users\\quickCoder\\Desktop\\Hello.java");

I made sure the file path entered is the actual path the Java class I have written.

7
  • One of your objects is null, and you are trying to access it. Commented Aug 13, 2015 at 1:08
  • Just ran the code in Netbeans( I was running on Eclipse)...it worked ! Commented Aug 13, 2015 at 1:17
  • Q: Are you sure "compiler" isn't null on return from "ToolProvider.getSystemJavaCompiler()"? Commented Aug 13, 2015 at 1:28
  • @paulsm4 Obviously it is null. There is no other explanation. Commented Aug 13, 2015 at 1:30
  • Nothing is "obvious". I suspect the OP was might be focussed on "compiler.run()", and wanted to ask if he checked for "compiler == null". That might have lead to JavaCompiler javac = new EclipseCompiler(); (and a successful "javac.run(...)"), or any of the other options discussed below. Commented Aug 13, 2015 at 1:36

2 Answers 2

3
  1. I suspect the problem is that "compiler" is being returned as "null" in your Eclipse environment, but non-null in your NetBeans environment.

  2. Note:

http://blog.nobel-joergensen.com/2008/07/16/using-eclipse-compiler-to-create-dynamic-java-objects-2/

However to gain access to the JDK compiler you need to run your application from the JDK, and since this is not default behaviour, I choose to use the Eclipse compiler instaid. (Besides the Eclipse compiler share the same interface, so the two compilers should behave similar).

  1. The same link discusses how to specify using the Eclipse compiler if you want to run in the Eclipse environment.

  2. However, you can point to any JDK:

How to set classpath when I use javax.tools.JavaCompiler compile the source?

Sign up to request clarification or add additional context in comments.

Comments

1

The compiler is part of the JDK. If you run this code in a JRE it will fail. So will the whole technique. Rethink your requirement.

2 Comments

Will it fail with a NullPointerException? Or is this an overall suggestion?
@SotiriosDelimanolis ToolProvider.getSystemJavaCompiler() will return null. Subsequent unchecked usage will of course throw an NPE, but that's the programmer's fault.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.