You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中「Cannot resolve method 'run(java.lang.Class, java.lang.String[])'」错误及main方法红下划线但应用正常运行问题求助

Fixing "Cannot resolve method 'run(java.lang.Class, java.lang.String[])'" in Spring Boot (IDE False Positive)

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:
    1. Go to File > Invalidate Caches...
    2. Select Invalidate and Restart (don’t worry, this won’t delete your code)
  • For Eclipse:
    1. Go to Project > Clean
    2. Check your Spring Boot project, click OK, then restart Eclipse

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 the SpringApplication class.

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 to File > Project Structure > Project and make sure the SDK and Language Level match your Spring Boot version’s requirements.
  • Eclipse:
    Go to Project > Properties > Java Build Path > Libraries and 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:59:07