Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

  • like image 256
    Leonardo Vinicius Maciel Avatar asked Jun 01 '14 21:06

    Leonardo Vinicius Maciel


    3 Answers

    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.

    like image 167
    Vladimír Schäfer Avatar answered Oct 19 '22 10:10

    Vladimír Schäfer


    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.

    like image 2
    Dimitar II Avatar answered Oct 19 '22 10:10

    Dimitar II


    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.

    like image 2
    Matthias Braun Avatar answered Oct 19 '22 11:10

    Matthias Braun