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

33,027

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

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

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

Solution 4

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!

Solution 5

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.

Share:
33,027
Leonardo Vinicius Maciel
Author by

Leonardo Vinicius Maciel

I love to find and fix bugs and also practice code challenges. Currently enriching my skills and focusing on web development (although I really like desktop development) with NodeJs and React. I had a good knowledge of Ruby, Ruby on Rails, C #, ASP.net, Python, Django, Java and Shell for a long time (also already programmed in Lua, C ++, Visual Basic and C, but I don't remember anything of last three). As a result of web development I also have experience in HTML, CSS, JavaScript, JQuery, JSON, YAML, MongoDB, PostgreSQL, MySQL and SQLite. Adoro encontrar e corrigir bugs além de também praticar desafios de código. Atualmente enriquecendo minhas habilidades e focando em desenvolvimento web (apesar de gostar muito do desenvolvimento desktop) com NodeJs e React. Há muito tempo tinha bons conhecimentos de Ruby, Ruby on Rails, C #, ASP.net, Python, Django, Java e Shell (também já programado em Lua, C ++, Visual Basic e C, mas não me lembro de nada dos últimos três). Como consequência do desenvolvimento web também possuo experiência em HTML, CSS, JavaScript, JQuery, JSON, YAML, MongoDB, PostgreSQL, MySQL e SQLite.

Updated on December 27, 2020

Comments

  • Leonardo Vinicius Maciel
    Leonardo Vinicius Maciel over 3 years

    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.