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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29668849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:24:15  来源:igfitidea点击:

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

javaeclipseclasspath

提问by Nishant Nawarkhede

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

我正在尝试运行 Java 应用程序,但出现以下错误,

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

找不到 javac 编译器;com.sun.tools.javac.Main 不在类路径上。也许 JAVA_HOME 没有指向 JDK

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

我经历了很多 SO 问题,发现解决方案是JAVA_HOME 应该指向 JDK 而不是 JRE

Then I have tried to print JAVA_HOME in command prompt,

然后我尝试在命令提示符下打印 JAVA_HOME,

enter image description here

在此处输入图片说明

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

我已经从我的电脑->属性->环境变量->系统变量中设置了这个JAVA_HOME,如下所示,

enter image description here

在此处输入图片说明

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

我还使用首选项在 Eclipse 中添加了新变量,

enter image description here

在此处输入图片说明

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

最后我仍然遇到同样的错误。JAVA_HOME 有什么问题?

Update :

更新 :

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

eclipse-preferences-installed jres仅存在一个条目存在并且其是JDK和选择

enter image description here

在此处输入图片说明

And under project propertiesjava build path-librariesthere is JRE System Liberary [jdk1.8.0_31]is used.

和下project propertiesjava build path-librariesJRE System Liberary [jdk1.8.0_31]使用。

enter image description here

在此处输入图片说明

Update 1 :

更新 1:

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

C:\Program Files\Java\jdk1.8.0_31有名为jre. 该jre文件夹是否导致此问题?我可以删除这个文件夹吗?有没有办法在项目中只添加 jdk 库?

采纳答案by Aaron Digulla

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.

Eclipse 是一个 IDE,因此,它(至少)有两个 Java 版本:一个用于运行 ( JAVA_HOME) 和一个用于运行应用程序的 JVM。两者不必相同。

So to fix your problem, you need to look into Eclipse's preferences, specifically Installed JREswhich 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.

因此,要解决您的问题,您需要查看 Eclipse 的首选项,特别是Installed JREs它为您提供了一个 Java VM 列表,Eclipse 将使用这些列表从项目中运行 Java 代码。我的猜测是那里会有几个条目,默认值将是 JRE 而不是 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.

确保列表中有 JDK,然后转到您的项目。在项目中,您可以在Java Build Path->下选择要使用的 Java VM Libraries

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

[编辑]仔细查看最后一个屏幕截图:您已将 Eclipse 配置为使用C:\Program Files\Java\jdk1.8.0_31\jre,这意味着您已将其指向 JDK 内的 JRE。使用C:\Program Files\Java\jdk1.8.0_31替代(不含\jre末)。

[EDIT 2]If you delete the jrefolder, Java will stop working. Any JDK also contains a JRE. The JRE contains rt.jarwith String.classand the like. The Java compiler is in tools.jarwhich is in the JDK.

[编辑 2]如果删除该jre文件夹,Java 将停止工作。任何 JDK 还包含一个 JRE。JRE 包含rt.jarwithString.class等。Java 编译器tools.jar位于 JDK 中。

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

如果在 Eclipse 中重新创建 JRE 条目没有帮助,则必须手动将其添加到类路径中。

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

您可以使用变量(“添加变量...”)来确保 Eclipse 在您切换到新的/不同的 JRE 时更新路径。JAVA_HOME用扩展试试lib/tools.jar

回答by Aneesh Kumar Mishra

  1. In Eclipse click Run->External Tools->External Tools Configurations
  2. Click JRE tab
  3. Click Installed JREs... button
  4. Click Add button (select Standard VM, where applicable)
  5. Click Directory Button
  6. Browse to your JDK version (not JRE) of your installed Java (eg: C:\Program Files\Java\jdk1.7.0_04)
  7. Click Finish.
  8. Re-run Ant script - have fun!
  1. 在 Eclipse 中点击 Run->External Tools->External Tools Configurations
  2. 单击 JRE 选项卡
  3. 单击已安装的 JRE... 按钮
  4. 单击添加按钮(选择标准 VM,如果适用)
  5. 单击目录按钮
  6. 浏览到已安装 Java 的 JDK 版本(不是 JRE)(例如:C:\Program Files\Java\jdk1.7.0_04)
  7. 单击完成。
  8. 重新运行 Ant 脚本 - 玩得开心!