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