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

8

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.

Unesco answered 20/3, 2013 at 0:52 Comment(4)
You probably need the JDK to use development tools like a compiler... just a guess.Wichita
Careful with titles. This question has nothing to do with "Java compiler not working".Amoral
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.Wichita
The JasperReports package needs a Java compiler, and can run on the JRE using the java based Eclipse java compiler.Berner
A
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");

Ambroseambrosi answered 7/10, 2013 at 20:17 Comment(1)
@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.Ambroseambrosi
E
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);
Eryn answered 5/6, 2014 at 6:10 Comment(0)
I
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

Ina answered 20/3, 2013 at 0:56 Comment(6)
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""Wichita
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".Wichita
well in that case is their anyway to compile a file on a jreUnesco
@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?Wichita
but i dont see the point of a method that only works on some platforms. How does eclipse compile code with a jreUnesco
@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.Wichita
W
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.)

Weizmann answered 13/5, 2015 at 7:3 Comment(0)
R
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 ]

Reseat answered 4/12, 2016 at 4:46 Comment(0)
K
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.

Kaltman answered 18/7, 2016 at 4:56 Comment(0)
V
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).

Vassaux answered 7/4, 2018 at 15:44 Comment(0)
E
0

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

Effeminate answered 15/10, 2018 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.