Could not find javac compiler com.sun.tools.javac.Main not related to classpath path error

I try to run a Java application and I get the following error,

Unable to find javac compiler; com.sun.tools.javac.Main the classpath is not included. Maybe JAVA_HOME is not pointing to JDK

I went through many SO questions and found a solution. JAVA_HOME should point to the JDK, not the JRE .

Then I tried to print JAVA_HOME on the command line,

enter image description here

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

enter image description here

I also added a new variable in eclipse using settings like

enter image description here

And finally, I still get the same error. What happened to JAVA_HOME?

Update:

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

enter image description here

And under the project properties java build path - libraries , the JRE System Liberary [jdk1.8.0_31] .

enter image description here

Update 1:

C:\Program Files\Java\jdk1.8.0_31 there is a folder named jre . jre this jre folder cause this problem? Can I delete this folder? Is there a way to add only jdk libary to the project?

+6
source share
1 answer

Eclipse is an IDE and, as such, has (at least) two versions of Java: the one that it uses to run ( JAVA_HOME ) and the JVM that it uses to run your application. These two should not be the same.

So, to fix your problem, you need to study the Eclipse settings, in particular the Installed JREs , which gives you a list of Java virtual machines that Eclipse will use to run Java code from projects. I assume that there will be several records, and by default there will be JRE instead of JDK.

Make sure you have the JDK in the list, and then go to your project. In the project, you can choose which Java virtual machine to use in the Java Build PathLibraries .

[EDIT] Take a look at the last screenshot: you configured Eclipse to use C:\Program Files\Java\jdk1.8.0_31\jre , which means that you pointed it to a JRE inside the JDK. Instead, use C:\Program Files\Java\jdk1.8.0_31 (without \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 located in tools.jar , which is in the JDK.

If recreating the JRE entry in Eclipse does not help, you will have to add it manually to the class path.

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

+8
source

Source: https://habr.com/ru/post/985246/


All Articles