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.

oneCoderToRuleThemAll
  • 834
  • 2
  • 12
  • 33
  • One of your objects is null, and you are trying to access it. – 10 Replies Aug 13 '15 at 01:08
  • Just ran the code in Netbeans( I was running on Eclipse)...it worked ! – oneCoderToRuleThemAll Aug 13 '15 at 01:17
  • Q: Are you sure "compiler" isn't null on return from "ToolProvider.getSystemJavaCompiler()"? – paulsm4 Aug 13 '15 at 01:28
  • @paulsm4 Obviously it *is* null. There is no other explanation. – user207421 Aug 13 '15 at 01: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. – paulsm4 Aug 13 '15 at 01:36
  • @paulsm4 The line in question is `compiler.run(...)`, and the only variable being dereferenced in the line is `compiler`. It is therefore null. QED. BTW I find much of computing extremely obvious. – user207421 Aug 13 '15 at 01:40
  • @ EJP: what might be "obvious" to you (or me) might *not* be obvious to the guy (or gal) asking the question. It's important to give them a good answer. It's even *more* important to help them ask the right questions about the problem at hand. In this case, one question is "Gee, did you look at 'compiler'?" And another is "Gee, why might compiler be null?" I have no reason to believe the OP didn't already know this. But it's *essential* to ask. First. – paulsm4 Aug 13 '15 at 03:54

2 Answers2

4
  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?

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
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.

user207421
  • 305,947
  • 44
  • 307
  • 483