Java Fatal Error: Unable to find package java.lang

2019-01-25 18:37发布

问题:

I'm getting this error

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

in IntelliJ when I try to build/compile any project. 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.

In IntelliJ I already:

  • Restarted, reinstalled
  • Invalidate Caches / restarted
  • Deleted / readded the JDK in Project JDK
  • Deleted / readded the JDK in Platform Settings
  • Rebuild / recompile

    In Windows I already:

  • Deleted / Reinstalled Java SDK
  • Deleted / Readded the JAVA_HOME, PATH and CLASSPATH on System Variables
  • 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...
    Here you 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.

  • 回答1:

    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):

    C:\Users\myuser\jdk1.8.0_20

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

    $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.

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

    C:\Programs\jdk1.8.0_20

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



    回答2:

    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:

    • 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

    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

    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.



    回答3:

    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.List for example.

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



    回答4:

    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.



    回答5:

    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).

    So the following has to be done to fix in intellij 1. 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

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



    回答6:

    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.

    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.

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