25

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.

jahroy
  • 22,322
  • 9
  • 59
  • 108
Josh Sobel
  • 1,268
  • 3
  • 14
  • 27
  • 2
    You probably need the JDK to use _development_ tools like a compiler... just a guess. – jahroy Mar 20 '13 at 00:56
  • 3
    Careful with titles. This question has nothing to do with "Java compiler not working". –  Mar 20 '13 at 01:03
  • 1
    I think you should anser the question by @nneonneo. There's a good chance he (or somebody else) could suggest a different approach (if they know what you want to do). Otherwise we can just tell you that your current approach will **not** work. – jahroy Mar 20 '13 at 01:05
  • The JasperReports package needs a Java compiler, and can run on the JRE using the java based Eclipse java compiler. – le3th4x0rbot Oct 07 '13 at 23:56

8 Answers8

11

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

Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39
  • @PSR- actually i am not Explicitly specifying the version of java.exe, i am just showing the path of JDK... And doing so it will work definitely. – Akhilesh Dhar Dubey Nov 29 '13 at 06:44
5

Here is how to run the Java compiler from your application when there is no JDK installed.

First, include the tools.jar file from a JDK with your Java app and put tools.jar in your classpath. Oracle probably won't like you doing that. But, there is legal work-around. You get the tools.jar file from the free JDKs offered by openjdk.org (openjdk), RedHat (IcedTea), or Azul Systems (Zulu).

Next, instead of using ToolProvider.getSystemJavaCompiler() and the JavaCompiler class, call the compiler located in tools.jar directly. Below is snippet of code:

String classpath = ...; // make sure tools.jar is in this path 
String sourcepath = ...; // path to your sources
String putputpath = ...; // directory for generated class files
String filepath = ...; // file path the file you want to compile

String[] args = new String[] {
"-classpath", classpath,
"-sourcepath", sourcepath,
"-d", putputpath,
filePath
};
com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
int compileStatus = javac.compile(args);
4

I think this is the problem .Explicitly specifying the version of java.exe you're using as the one in your JDK directory.

see here for details

Community
  • 1
  • 1
PSR
  • 39,804
  • 41
  • 111
  • 151
  • From that link: "_Bases on the SDN comments, I agree the problem can be reproduced if the given test program is run with JRE instead of JDK. Assuming that this is the problem the submitter ran into, I am change the Close substatus from "Not reproducible" to "Not a defect"_" – jahroy Mar 20 '13 at 00:57
  • 1
    The OP has indicated that he is **not** using a JDK, but rather a JRE. The link you've provided states that `null` is the expected return value when using a JRE. Therefore, the bug was flagged as "_not a defect_". – jahroy Mar 20 '13 at 01:00
  • well in that case is their anyway to compile a file on a jre – Josh Sobel Mar 20 '13 at 01:01
  • 1
    @JoshSobel - Well... If the compiler is a tool that is only included in the JDK, then it makes perfect sense that you wouldn't be able to use a compiler without installing a JDK. Where do you expect the compiler to come from if the user hasn't downloaded and installed one? – jahroy Mar 20 '13 at 01:03
  • but i dont see the point of a method that only works on some platforms. How does eclipse compile code with a jre – Josh Sobel Mar 20 '13 at 01:08
  • 1
    @JoshSobel - Eclipse comes with its own compiler (and presumeably some JDK). I don't see why it's hard to accept the fact that Java can't invoke a compiler if the computer being used doesn't have a compiler installed. – jahroy Mar 20 '13 at 01:16
3

Another solution is from: - http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7181951

Copy tools.jar in JDK_HOME/lib/ into JRE_HOME/lib/. At least to me, it solved my issue magically!

(I did nothing recommended as above. I just copied it there.)

user3509406
  • 389
  • 1
  • 10
3

Just copy tools.jar file from /lib to It works

You can obtain by System.out.println( System.getProperty( "java.home"))

Most of time it is like C:\Program files\Java\jre(version) [ for windows ]

Mr. Suryaa Jha
  • 1,516
  • 16
  • 10
0

here is simple solution that worked for me

I just changed the jre System library to .....Program Files\Java\jdk1.7.0_55\jre instead of ....Program Files\Java\jdk1.7.0_55\bin and it worked for me.

Arshad Ali
  • 93
  • 1
  • 6
0

On a Mac this worked for me:

  System.setProperty("java.home", "/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home");
  javax.tools.JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

(It does not work for idk 10 for some reason).

clankill3r
  • 9,146
  • 20
  • 70
  • 126
0

I had both JRE and JDK in my buildPath...i just removed the JRE and it fixed.