ToolProvider.getSystemJavaCompiler() returns null - usable with only JRE installed?
Solution 1
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");
Solution 2
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);
Solution 3
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
Solution 4
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 ]
Solution 5
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.)
Related videos on Youtube
Josh Sobel
Updated on October 15, 2022Comments
-
Josh Sobel over 1 year
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 about 11 yearsYou probably need the JDK to use development tools like a compiler... just a guess.
-
Admin about 11 yearsCareful with titles. This question has nothing to do with "Java compiler not working".
-
jahroy about 11 yearsI 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.
-
trognanders over 10 yearsThe JasperReports package needs a Java compiler, and can run on the JRE using the java based Eclipse java compiler.
-
-
jahroy about 11 yearsFrom 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 about 11 yearsThe 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". -
Josh Sobel about 11 yearswell in that case is their anyway to compile a file on a jre
-
jahroy about 11 years@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?
-
Josh Sobel about 11 yearsbut i dont see the point of a method that only works on some platforms. How does eclipse compile code with a jre
-
jahroy about 11 years@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.
-
Akhilesh Dhar Dubey over 10 years@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.