13

In a new project in IntelliJ I have set up a 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 just fine (from within the IDE, too) but code completion does not work.

Any ideas? I'd like to ask a few other users before creating a ticket on their bugtracker...

7
  • If you are on Windows, is your environment variables properly setup? Can you double check which version of Java is being used to compile the project?
    – Konstantin
    Commented Aug 8, 2014 at 14:21
  • I am on Linux. Currently there's only one JRE/JDK installed on my system and it says "Java(TM) SE Runtime Environment (build 1.8.0_11-b12)" Commented Aug 8, 2014 at 14:54
  • Is your IntellJ version fairly recent? At least 13.1.x?
    – PM 77-1
    Commented Aug 8, 2014 at 17:32
  • Are you sure the Project SDK that is selected points to JDK 8, and that your modules Language level is also using the correct JDK?
    – mkobit
    Commented Dec 18, 2014 at 19:19
  • 1
    I just ran into this as well with 2017.1.3 My gradle project is definitely set to JDK 8 and it was even suggesting OptionalDouble and other classes from the same package. But it kept autocorrecting to the guava version of Optional and removing any manually added import for java.util.Optional. I fixed it by exiting, gradle cleaning, killing the IDE, editing the file manually outside of intellij, and then restarting intellij. The problem finally went away. It looks to me that the autocomplete data can get in a weird state sometimes. Commented Jun 6, 2017 at 12:22

6 Answers 6

9

I finally figured this out. For whatever reason, there was an entry for java.util.Optional in the exlusions under Editor, General, and Auto Import.

Removing that did the trick. No idea how or why it ended up being there. Doesn't sound like anything I would want to do.

3
  • Naah this was not the issue. It seems like usage detail was not present for Optional so ide suggested to change the preference to warning rather error. I chose that and now it shows a warning and not error anymore. Commented Jul 28, 2017 at 17:11
  • cleaning intellij ide cache didn't solve the problem for me. then I checked if I have this as an exclusion there I found it!!! but I didn't add Optional in exclusion list. I'm a guy who used to code in eclipse and now for some reason I switched to intellij. It's really not worth the effort trying to use this intelligent ide that can't solve usual stupid problems. Commented Sep 1, 2020 at 10:10
  • Thanks! I think I messed up using SonarLint and somehow added this config by mistake
    – Alejandro
    Commented Aug 8, 2022 at 13:53
7

Stupid IntelliJ.

I have a SpringBoot project which 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 without changing anything!

  • I cleared IntelliJ cache.

Although it was a maven project it was already using JDK8,

  • I have changed the Project Settings, and said (again) to use JDK8,
  • Maven re-import, Cleaned the project, Build->Rebuild project, and it magically worked again
2

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

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

1
  • As I mentioned in the question and comments, I had set up only v8 from the start. Unfortunately I don't remember how I finally fixed this... Commented Jun 2, 2016 at 11:00
1

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

I've also first set Java compiler to 8 and 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

I ran into this recently. The issue was related to my Project SDK setting. Didn't have to clear any caches.

The issue shows with my Project SDK set to 1.8 (1.8.0_202). The issue was fixed by changing it to 11 (11.0.6). Also confirmed that the issue reappears when I switched it back to 1.8.

0

In java 8 some functions of Optional class not supported such as isEmpty, ifPresentOrElse, or, stream, orElseThrow. You should check whether you use them or not. If you want to use these functions you can use java 11.

Not the answer you're looking for? Browse other questions tagged or ask your own question.