spring-bootslf4jspring-initializr

spring initializr: Cannot import org.slf4j.LoggerFactory for Gradle projects


I'm trying to learn how to build "Spring Web" web-applications using IntelliJ IDEA, and I'm encountering a very frustrating issue when I try to create a new project using "Spring Initializr".

For some reason when I choose to create a "Gradle Project", Java or Kotlin, I'm not able to import org.slf4j.LoggerFactory

If I manually add the import statement, it doesn't resolve, and if I try to get InteliJ to automatically add the import, it instead adds the following...

import com.sun.org.slf4j.internal.LoggerFactory

This satisfies the editor, but it won't compile. It errors with...

Symbol is declared in module 'java.xml.crypto' which does not export package 'com.sun.org.slf4j.internal'

If instead I create a "Maven Project", either Kotlin or Java, then it works as expected.

I'm keeping the other settings in spring initializr set to default values, Spring Boot=2.4.2, Java=11

IntelliJ IDEA says there are no updates available.

I've made various attempts to manual add the slf4j dependencies to build.gradle (for Java) or build.gradle.kts (for Kotlin), and nothing I've done has made any difference.

I've also tried various different options for Spring Boot and Java version, and again it has made no difference.

Update...

I've confirmed that it only happens on one of my computers, my primary development desktop computer. My laptop is working fine.

I can't find anything that's different between the 2 IDEA installations.

Things I've tried so far to fix it...

  1. Invalidate Caches / Restart
  2. Manage IDE Settings -> Restore Default Settings
  3. Removing and re-adding JDK's
  4. Uninstalling and re-installing IntelliJ IDEA

I've tried everything I can think of to fix this, but the problem persists.

Basically I can't use logging in any new Gradle Spring projects. It just won't load the org.slf4j library.

Update 2...

The nature of the problem isn't exactly what I thought it was initially. It turns out the code compiles and runs just fine with the correct import, but the IDE is reporting it as unresolved.

So it seems it's just an issue with the IDE's intellisense. This seems less serious, but it's still quite inconvenient having the editor incorrectly showing errors.


Solution

  • After identifying that the problem was just with the IDE editor intellisense, rather than with the code dependencies or project, I was eventually able to resolve it by closing the IDE, and then deleting the system folder "C:\Users<user>\AppData\Local\JetBrains\IntelliJIdea2020.3"

    After restarting IntelliJ IDEA, the folder was re-created automatically and the issue resolved itself.