Spring Boot中「Cannot resolve method 'run(java.lang.Class, java.lang.String[])'」错误及main方法红下划线但应用正常运行问题求助
Hey there! I’ve dealt with this exact confusing issue a bunch of times—your Spring Boot app runs perfectly fine, but your IDE (like IntelliJ IDEA or Eclipse) keeps red-underlining the SpringApplication.run() call in your main method. Let’s walk through the most common causes and fixes:
1. IDE Cache/Index Glitch (Most Likely)
IDEs rely on cached indexes to analyze code, and sometimes these get corrupted or out of sync with your project. This is the #1 culprit for false positives like this.
- For IntelliJ IDEA:
- Go to
File > Invalidate Caches... - Select
Invalidate and Restart(don’t worry, this won’t delete your code)
- Go to
- For Eclipse:
- Go to
Project > Clean - Check your Spring Boot project, click
OK, then restart Eclipse
- Go to
2. Incomplete Dependency Sync
Even if your build tool (Maven/Gradle) has all the right dependencies, your IDE might not have imported them properly.
- Maven:
Right-click your project →Maven > Reload Project - Gradle:
Right-click your project →Gradle > Refresh Gradle Project
This forces your IDE to re-fetch and index all Spring Boot dependencies, including theSpringApplicationclass.
3. Wrong Import Statement
Double-check that you’re importing the correct SpringApplication class. Sometimes auto-import can pull in a random class with the same name from another package. Your main class should have these imports at the top:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
If you see any other SpringApplication import, delete it and replace with the one above.
4. Mismatched JDK Version
Spring Boot has strict JDK version requirements (e.g., Spring Boot 3.x needs JDK 17+). If your IDE is using a different JDK than what your project is configured for, it can cause false method resolution errors.
- IntelliJ:
Go toFile > Project Structure > Projectand make sure theSDKandLanguage Levelmatch your Spring Boot version’s requirements. - Eclipse:
Go toProject > Properties > Java Build Path > Librariesand ensure the JRE System Library is the correct version.
Why This Happens
The key thing to remember: when you run your app, it’s using Maven/Gradle to handle compilation and dependency resolution. Your IDE’s static code analysis is a separate system—sometimes it falls behind or has bugs that don’t reflect the actual runnable state of your project.
内容的提问来源于stack exchange,提问作者Marcel

