Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find a javac compiler com.sun.tools.javac.Main is not on the classpath error

I am trying to run java application and I am getting following error,

Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath. Perhaps JAVA_HOME does not point to the JDK

I have gone through many SO questions and found solution is JAVA_HOME should be point to JDK, not JRE.

Then I have tried to print JAVA_HOME in command prompt,

enter image description here

I had set this JAVA_HOME from my computer->properties->env variables -> system vars as below,

enter image description here

I had also added new variable in eclipse using preferences as,

enter image description here

And finally I am still getting the same error. Whats wrong with JAVA_HOME ?

Update :

In eclipse-preferences-installed jres there is only one entry is present and which is jdk and selected,

enter image description here

And under project properties java build path-libraries there is JRE System Liberary [jdk1.8.0_31] is used.

enter image description here

Update 1 :

C:\Program Files\Java\jdk1.8.0_31 have folder named jre. Is that jre folder causing this issue ? Can I remove this folder ? Is there is any way to add only jdk liberary in project ?

like image 823
Nishant Nawarkhede Avatar asked Apr 16 '15 08:04

Nishant Nawarkhede


Video Answer


1 Answers

Eclipse is an IDE and as such, it has (at least) two Java versions: The one which it uses itself to run (JAVA_HOME) and a JVM which it uses to run your application. The two don't need to be the same.

So to fix your problem, you need to look into Eclipse's preferences, specifically Installed JREs which gives you a list of Java VMs which Eclipse will use to run Java code from projects. My guess is that there will be several entries there and the default will be a JRE instead of a JDK.

Make sure you have a JDK in the list and then go to your project. In the project, you can select which Java VM to use under Java Build Path -> Libraries.

[EDIT] Look closely at the last screenshot: You've configured Eclipse to use C:\Program Files\Java\jdk1.8.0_31\jre which means you've pointed it at the JRE inside of the JDK. Use C:\Program Files\Java\jdk1.8.0_31 instead (without the \jre at the end).

[EDIT 2] If you delete the jre folder, Java will stop working. Any JDK also contains a JRE. The JRE contains rt.jar with String.class and the like. The Java compiler is in tools.jar which is in the JDK.

If recreating the JRE entry in Eclipse doesn't help, you'll have to add it manually to the classpath.

You can use a variable ("Add Variable...") to make sure Eclipse updates the path when you switch to a new/different JRE. Try JAVA_HOME with the extension lib/tools.jar

like image 150
Aaron Digulla Avatar answered Sep 16 '22 12:09

Aaron Digulla