ToolProvider.getSystemJavaCompiler() returns null - usable with only JRE installed?

Josh Sobel picture Josh Sobel · Mar 20, 2013 · Viewed 17.5k times · Source

I am trying to use the JavaCompiler class:

When I call ToolProvider.getSystemJavaCompiler() it returns null.

I think this is because I'm using a JRE instead of a JDK.

The problem is I want it to run on all platforms regardless of weather the user is using a JRE or a JDK.

If anyone knows how to fix this, or an alternative method to use please comment.

Any help would be appreciated.

Answer

Akhilesh Dhar Dubey picture Akhilesh Dhar Dubey · Oct 7, 2013

ToolProvider.getSystemJavaCompiler() is not available.

Is tools.jar missing from the classpath?

Set class path to the tools.jar file which can found in jdk\jre directory.

System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_02");