Arian
Arian

Reputation: 3241

JavaCompiler from inside a jar

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

Upvotes: 1

Views: 406

Answers (3)

Akash Yadav
Akash Yadav

Reputation: 2421

As Per my understanding, JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); Returns the Handle to java compiler only if its beign executed under and JDK distribution.

if you are using it under JRE (as normally we execute under JRE only) it would return null, could you please confirm that we are calling it from JDK or it contains the appropriate jar file in place

Upvotes: 0

aioobe
aioobe

Reputation: 420921

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.jar from some basic JRE installation, while when you execute it from an IDE you're most likely using an rt.jar from the JDK.

You could do System.out.println(System.getProperty("java.home")); to debug this.

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168815

It it is your own deployment (to your own machine), the problem can be solved by explicitly adding the tools.jar of the SDK to the run-time class-path of your application (e.g. mention it in the manifest).

Alternative strategies can be seen (or are at least hinted at) in the STBC configuration. If you need more info. on those, let me know.

Upvotes: 0

Related Questions