JRE classes like java.lang.String cannot be resolved.

已回答

Dear All,

I have imported a maven project from a svn repository into the Intellij Idea 9.0 community edition. The main project has a number of sub-projects. The projects were imported as modules in IntelliJ. When I rebuild the top project using Build -->Rebuild project I see errors like java.lang.Object cannot be resolved. Similarly other JRE/ java SDK classes cannot be resolved. I have configured a SDK for the module project. This SDK has the src files as well as the compiled JRE class files for Object, String etc.

My question is .. Why is IntelliJ not finding these class files inspite of the SDK being listed as a dependency for the module.
All input is appreciated, What am I missing ?

--Thanks,
Rohit Kelapure

3
Avatar
Permanently deleted user

Hello Rohit,

Welcome to the IntelliJ IDEA community.

Sounds like there is a misconfiguration in the project setup somewhere with the JDK such that IntelliJ IDEA is not finding the SDK.

First, verify you have a JDK set up:
1) Open up the "Project Structure" dialog (ctrl+alt+shift+S).
2) On the far left, select "SDKs" under the "Platform Settings" section.
3) In the middle pane will be a list of all the SDKs you have configured. If you have none, or do not have the one you need, click the '+" icon at the top, or type Alt+Insert. Select JSDK and add a Java SDK. Adding the source as well as the classes will allow IDEA to provide more inline help such as JavaDocs and stepping into the code during debugging. Give the JDK a name. In the rest of the information below, I'll assume the name is "JDK 1.6". It sounds like you may already have this, but I just want to be sure you have added your JDK as a SDK and not as a standard library.
4) Be sure to hit apply so you don't lose your changes.

Second, make sure your project has a JDK configured.
1) In the same Project Structure" dialog (ctrl+alt+shift+S), in the far left pane, select "Project" within the "Project Settings" section.
2) On the right, make sure where it says "Project SDK", a Java SDK, such as the "JDK 1.6" we created above is listed. Note that you could also create a new JDK SDK definition here, by clicking the "New" button, rather than where we did above.
3) Set the "Project language level" as appropriate.
4) Be sure to hit apply so you don't lose your changes.

Third, make sure you module(s) have a JDK listed.
1) In the same Project Structure" dialog (ctrl+alt+shift+S), in the far left pane, select "Modules" in the "Project Settings" section.
2) Select each module you have in the have created in the middle pane, and then select the "Dependencies" tab in the far right pane.
3) Make sure the JDK is specified in the "module SDK". Typically you can leave this set as "Project SDK" unless your module needs a different SDK. Just to verify one comment you made... you said have the SDK "listed as a dependency for the module." I just want to make sure you do not mean it is listed in the list of dependencies on the "Dependencies" tab. That would make it a standard library. It should be specified in the "Module SDK".

Save your changes and you should be all set.

8

In my case i was trying to build project in IntelliJ IDEA 2021.1.3 (but tried also in another versions like 2020.2.4) with IBM JAVA 1.8.0_181 (build 8.0.5.20). I tried everything that is described in here or anywhere on the internet, but still was getting the error:

java cannot access java.lang.object class file for java.lang.object not found

After this i check if all the classpath was added in

Project Structure > Platform Settings > SDKs > Classpath

and compare it with my another functional project settings and find out that one JAR was missing. I have to add it manually.

In my case i have to add

<java_home>\jre\bin\default\jclSC180\vm.jar

Did not find this problem nowhere else and did not find the reason why this happened. Hope this helps to anyone who is struggling with similar problem.

4
Avatar
Permanently deleted user

I can be another issue than missconfigured JDK, for me it took some time to figure out what is happening (Invalidate Caches / Restart solved the problem but the cause is more interesting).

I have multiple projects, some use java 8, some java 15.
Sometimes I had projects open simultaneously in different windows.


I guess the cause is some shared indexing or caching issue in IntelliJ.
btw, this is my IDE version, if relevant:

IntelliJ IDEA 2020.3.3 (Ultimate Edition)
Build #IU-203.7717.56, built on March 15, 2021

1

Thank you I had the same issue

0

Make sure you are typing String instead of string

0

Thank you Mark Vedder! So many answers online suggest "Invalidate Caches / Restart" but that didn't help me at all. Your solution finally resolves my issue.

0
Avatar
Permanently deleted user

Solves my problem!

0

Thanks R K that solved exactly the same problem for me!

0

The IDE is deeply corrupted in my opinion. Shared Indexes, shared configurations, you can't open  two different configuration windows in two open projects windows (you can't compare your configuration between projects without closing one and open another). Does not find obvious imports in classes albait they are defined in the class itself and the downwards hierarchy to the class is straight forward. And this are just the basics of an IDE. All in all so many violations of separation of concerns on so much levels, its for me not understandable why I should pay for it, I would not use it at all if I didn't have to.

0

Sorry for the inconvenience.

Please see if https://stackoverflow.com/a/61180376/104891 helps.

If not, please contact support at https://intellij-support.jetbrains.com/hc/requests/new with the logs attached (Help | Collect Logs and Diagnostic Data). I'm sure we'll be able to help you with this issue.

You are paying for support too, so that there is someone to help you in the situations like this.

0

请先登录再写评论。