my JavaCompiler returns a null pointer when called from a jar file.
Everything works fine if I run my program with the JavaCompiler from command line.
So the JDK is properly installed on my OS.
Why isn’t it working?
Code:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<String> options = Arrays.asList( new String[] { "-d", currentDir+"/cache/","-sourcepath",currentDir+"/srcss/"} );
Iterable<? extends JavaFileObject> compUnits = fileManager.getJavaFileObjects(fRun);
Boolean compRes = compiler.getTask(null, fileManager, null, options, null, compUnits).call();
You need to make sure you’re using the same runtime when launching the application as a jar.
When launching it as a stand alone application you’re probably using
rt.jarfrom some basic JRE installation, while when you execute it from an IDE you’re most likely using anrt.jarfrom the JDK.You could do
System.out.println(System.getProperty("java.home"));to debug this.