0

This is my code I am getting nullpointer exception at JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); line as per given example on stackoverflow I got some said to set "java.home" explicitly however I am getting same issue.

File dir = new File("d://Hello.java");

File[] javaFiles = dir.new FilenameFilter() {
    public boolean accept(File file, String name) {
        return name.endsWith(".java");
    }
});

String home= System.getProperty("java.home");

System.out.println(""+home.replace("\\jre",""));

System.setProperty("java.home", home);

JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();


DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(diagnostics, Locale.US,Charset.forName("UTF-8"));
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(javaFiles));
javaCompiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits).call();

for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
    System.out.format("Error on line %d in %d%n", diagnostic.getLineNumber(), diagnostic.getSource().toString());
}        

fileManager.close();
Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27
  • 3
    Possible duplicate of [Null Pointer Exception while using Java Compiler API](http://stackoverflow.com/questions/2543439/null-pointer-exception-while-using-java-compiler-api) – Arnaud Jan 24 '17 at 10:10

2 Answers2

0

I think you might run into this known problem. Try to use JRE instead of JDK.

If you are already using JRE, then try to specifically include tools.jar into your library.

As stated by Jon Skeet in this answer

Community
  • 1
  • 1
Shawn Xiong
  • 470
  • 3
  • 14
  • I have tried but got same issue again and again ,already include tools.jar in classpath – Pawan Kumar Sharma Jan 24 '17 at 10:35
  • Oh OK, the piece of code that seems dodgy to me is set system property on the runtime `System.setProperty("java.home", home)`, I have never done that but in order not to fall into my own personal credulity fallacy, I would recommend you to have a thorough test on that bit. – Shawn Xiong Jan 24 '17 at 11:17
  • I was not using System.setProperty("java.home", home) in my code but I got an example on stackoverflow to use it, issue is still same if I remove it – Pawan Kumar Sharma Jan 24 '17 at 13:13
  • @Pawan I just did a quick check and it works. My environment is: Java 6, Windows 10. And are you running in Eclipse or other IDE? You can also check if JDK of your IDE is pointing to the correct place. – Shawn Xiong Jan 24 '17 at 14:24
  • I am using jdk 7 and running this program in Netbeans and windows 7 – Pawan Kumar Sharma Jan 24 '17 at 14:53
0

finally I got the issue ,this is because of different version of Tools.jar I was using different Tools.jar in my class path as I was using JDK 1.7 but using Tools.jar 1.6 version. Now it is fixed !!!!