Java 致命错误:无法在类路径或引导类路径中找到包 java.lang

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23984513/
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-14 09:33:14  来源:igfitidea点击:

Java Fatal Error: Unable to find package java.lang in classpath or bootclasspath

javaintellij-ideacompiler-errorside

提问by Leonardo Vinicius Maciel

I'm getting this error

我收到这个错误

Error:java: Fatal Error: Unable to find package java.lang in classpath or bootclasspath

错误:java:致命错误:无法在类路径或引导类路径中找到包 java.lang

in IntelliJ when I try to build/compile anyproject. Something that should be stated here is that NetBeans, Eclipse and even the javac command line can compile, build, and execute the same project without errors. This is why I suspect that the problem lies with IntelliJ, all other IDEs work well.

在 IntelliJ 中,当我尝试构建/编译任何项目时。这里应该说明的是,NetBeans、Eclipse 甚至 javac 命令行都可以毫无错误地编译、构建和执行相同的项目。这就是为什么我怀疑问题出在 IntelliJ 上,所有其他 IDE 都运行良好。

In IntelliJ I already:

在 IntelliJ 中,我已经:

  • Restarted, reinstalled
  • 重新启动,重新安装
  • Invalidate Caches / restarted
  • 使缓存无效/重新启动
  • Deleted / readded the JDK in Project JDK
  • 删除/重新添加了 Project JDK 中的 JDK
  • Deleted / readded the JDK in Platform Settings
  • 删除/重新添加了平台设置中的 JDK
  • Rebuild / recompile

    In Windows I already:

  • 重建/重新编译

    在 Windows 中我已经:

  • Deleted / Reinstalled Java SDK
  • 删除/重新安装 Java SDK
  • Deleted / Readded the JAVA_HOME, PATH and CLASSPATH on System Variables
  • 删除/重新添加系统变量上的 JAVA_HOME、PATH 和 CLASSPATH
  • Tried the above on User Variables
  • 在用户变量上尝试了上述操作
  • Deleted and reinstalled IntelliJ IDEA

    I already seached for issues like this on the Internet, here on Stack Overflow I found:
    Question #1| Question #2| Question #3| Question #4| Question #5
    And 30+ sites, and tried every answer...
    Hereyou can see my IntelliJ Project Structure the last time that I tried to build/compile

    I was using Java 1.8 when I got this error, after some hours, I installed Java 1.7 and the project worked fine. Maybe IntelliJ doesn't support Java 1.8?

    Anyway, it would be great if anyone knew a way to make Java 1.8 work with IntelliJ.

  • 删除并重新安装 IntelliJ IDEA

    我已经在互联网上搜索过这样的问题,我在 Stack Overflow 上找到了:
    问题 #1| 问题#2| 问题#3| 问题#4| 问题 #5
    和 30 多个站点,并尝试了每一个答案......
    在这里你可以看到我上次尝试构建/编译时我的 IntelliJ 项目结构

    我正在使用 Java 1.8 时出现此错误,几个小时后,我安装了Java 1.7 和该项目运行良好。也许 IntelliJ 不支持 Java 1.8?

    无论如何,如果有人知道让 Java 1.8 与 IntelliJ 一起工作的方法,那就太好了。

  • 采纳答案by Dimitar II

    The problem comes from IntelliJ bug (existing at least in version 14). It happens when your JDK directory is under the user profile directory. For example, JDK located in this directory (for Windows):

    问题来自 IntelliJ 错误(至少在版本 14 中存在)。当您的 JDK 目录位于用户配置文件目录下时,就会发生这种情况。例如,位于此目录中的 JDK(对于 Windows):

    C:\Users\myuser\jdk1.8.0_20

    C:\Users\myuser\jdk1.8.0_20

    will be stored in IntelliJ internal configuration (file 'jdk.table.xml') as:

    将存储在 IntelliJ 内部配置(文件“jdk.table.xml”)中:

    $USER_HOME$\jdk1.8.0_20

    $USER_HOME$\jdk1.8.0_20

    But your %USER_PROFILE% environment variable could point at some other (non default) location. Then IntelliJ will look at the wrong directory.

    但是您的 %USER_PROFILE% 环境变量可能指向其他(非默认)位置。然后 IntelliJ 会查看错误的目录。

    Possible fix: Install your JDK at different directory, which is outside your profile directory.Like:

    可能的解决方法: 将您的 JDK 安装在不同的目录中,该目录在您的配置文件目录之外。喜欢:

    C:\Programs\jdk1.8.0_20

    C:\Programs\jdk1.8.0_20

    Then register this JDK for use in your IntelliJ project settings.

    然后注册此 JDK 以在您的 IntelliJ 项目设置中使用。

    回答by A.Aleem11

    If this problem occurs when importing a project in netbeans then try to create a new project then check the properties of your new created project and match those properties with the project that you imported in my case there was a difference between target of android package version.

    如果在 netbeans 中导入项目时出现此问题,则尝试创建一个新项目,然后检查新创建的项目的属性,并将这些属性与您在我的情况下导入的项目匹配,android 包版本的目标之间存在差异。

    回答by Vladimír Sch?fer

    I just spent quite few hours on this and found a solution which might work for you as well. IntelliJ seems to have a bug which expects the JDK to be in a different directory than is specified. I followed these steps:

    我只是在这上面花了好几个小时,找到了一个也可能对你有用的解决方案。IntelliJ 似乎有一个错误,它期望 JDK 位于与指定目录不同的目录中。我按照以下步骤操作:

    • open intelliJ, click help -> show log in explorer
    • modify build-log/build-log.xml and change <priority value="info" />to <priority value="debug" />, you may have to run the editor as an administrator
    • run the compilation which fails with "Error:java: Fatal Error: Unable to find..."
    • open build-log/build.log and search for rt.jar, you should find it in a block with a set of other JRE libraries and the directory which points to the file should be wrong
    • 打开intelliJ,点击help -> show log in explorer
    • 修改 build-log/build-log.xml 并更改<priority value="info" /><priority value="debug" />,您可能需要以管理员身份运行编辑器
    • 运行编译失败并显示“错误:java:致命错误:无法找到...”
    • 打开build-log/build.log,搜索rt.jar,应该是在一个包含其他JRE库的块中,指向该文件的目录应该是错误的

    In my case the JDK is included in D:\Development\software\jdk1.8.0_20, while the log points to C:\Users\Vladimir\Development\software\jdk1.8.0_20

    在我的例子中,JDK 包含在 D:\Development\software\jdk1.8.0_20 中,而日志指向 C:\Users\Vladimir\Development\software\jdk1.8.0_20

    I fixed the issue by copying my JRE to the place which is expected in the log. And just like in your case this seems to only happen with JDK 1.8.

    我通过将我的 JRE 复制到日志中预期的位置来解决该问题。就像你的情况一样,这似乎只发生在 JDK 1.8 中。

    回答by shazzing

    From what I understood, debugging for hours, the problem is with the location where the jdk1.8 gets installed(/Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home), instead of under System/Library).

    据我了解,调试了几个小时,问题在于安装 jdk1.8 的位置(/Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home),而不是在 System/Library 下)。

    So the following has to be done to fix in intellij1. For an existing project, right click and select module settings, and SDK and add 1.8 as a version. 2. Using a terminal go to ~/Library/Preferences//options/. Remove the old JDK specific lines in jdk.table.xml. Update the default version in project.default.xml

    所以需要在intellij1中进行以下修复。对于现有项目,右键单击并选择模块设置和SDK并添加1.8作为版本。2. 使用终端转到 ~/Library/Preferences//options/。删除 jdk.table.xml 中旧的 JDK 特定行。更新 project.default.xml 中的默认版本

    Clean you project in intellij and build. It should use the new jdk and sync.

    在 Intellij 中清理您的项目并构建。它应该使用新的 jdk 和同步。

    回答by Matthias Braun

    I encountered this on Arch Linux 4.16.13 with IntelliJ 2018.1.4 after importing a Java project using Gradle. I made sure that there is a registered JDK in "Project Settings" -> "Project" but IntelliJ was unable to find the classes from the JDK, java.util.Listfor example.

    在使用 Gradle 导入 Java 项目后,我在 Arch Linux 4.16.13 和 IntelliJ 2018.1.4 上遇到了这个问题。例如,我确保在“项目设置”->“项目”中有一个已注册的 JDK,但 IntelliJ 无法从 JDK 中找到这些类java.util.List

    The solution was to add a new JDK entry at the same path as the existing JDK (/usr/lib/jvm/java-8-openjdkin my case). This will cause IntelliJ to put the files under jreonto the classpath whereas with the old JDK entry, only the files under libwere on the classpath.

    解决方案是在与现有 JDK 相同的路径中添加一个新的 JDK 条目(/usr/lib/jvm/java-8-openjdk在我的情况下)。这将导致 IntelliJ 将文件jre放在类路径下,而对于旧的 JDK 条目,只有lib在类路径下的文件。

    回答by Muhammad Mubashirullah Durrani

    Had the same problem. Simple fix to others in case the other answers are complex.

    有同样的问题。如果其他答案很复杂,则对其进行简单修复。

    Go to File -> Project Structure Under Project SDK, choose new and add another JDK.

    转到 File -> Project Structure 在 Project SDK 下,选择 new 并添加另一个 JDK。

    The default use in my case was 1.8 that was of build 181. I choose the 1.8.0_192 I had recently downloaded.

    在我的情况下,默认使用的是 181 版本的 1.8。我选择了我最近下载的 1.8.0_192。

    After applying, the project loads itself. When I re-ran the test, it works!

    应用后,项目自行加载。当我重新运行测试时,它起作用了!