IntelliJ: cannot find java.util.Optional

In a new project in IntelliJ, I installed Java 8 JRE / JDK and language level 8.0.

But for some reason, the IDE cannot find java.util.Optional . The project compiles and works very well (from the IDE too), but code completion does not work.

Any ideas? I would like to ask a few other users before creating a ticket on my bugtracker ...

+7
source share
4 answers

Stupid IntelliJ.

I have a SpringBoot project that worked just fine. Then, the next day, when I want to run it, I got this error: Caused by: java.lang.Error: Unresolved compilation problem: The type java.util.Optional cannot be resolved. It is indirectly referenced from required.class files Caused by: java.lang.Error: Unresolved compilation problem: The type java.util.Optional cannot be resolved. It is indirectly referenced from required.class files Caused by: java.lang.Error: Unresolved compilation problem: The type java.util.Optional cannot be resolved. It is indirectly referenced from required.class files Caused by: java.lang.Error: Unresolved compilation problem: The type java.util.Optional cannot be resolved. It is indirectly referenced from required.class files without changing anything!

  • I cleared the IntelliJ cache.

Although it was a Maven project, it already used JDK8,

  • I changed the project settings and said (again) to use JDK8,
  • Re-import Maven, Clean up the project, Build-> Rebuild the project, and it again magically works
+2
source

I finally figured it out. For some reason, there was an entry for java.util.Optional in the outputs in the "Editor", "General" and "AutoPort" sections.

Removing did the trick. I don’t know how and why he ended up there. Not like what I would like to do.

+3
source

Ubuntu had a similar problem. For me, the problem was that I used JDK1.7, and when I changed it to JDK1.8, everything worked fine.

Now it makes sense, since Optional is what was introduced in JDK1.8, before that the concept existed only in Guava: http://onelineatatime.io/optional-guava-and-java-8/

+2
source

Faced the same problem (in IntelliJ 2017.2.7) and solved it like this: Project Structure-> Modules-> Module SDK and there changed it to 1.8

I also first set the Java compiler to 8, and the Project Structure-> Project-> Project SDK and Project language level also set to 8 without success. In the end, the step I described finally fixed it.

0
source

Source: https://habr.com/ru/post/973547/


All Articles