使用IntelliJ IDEA 2017.3运行Gradle项目报错:找不到主类1.8求助
Hey there, let's work through this frustrating issue you're hitting with IntelliJ IDEA 2017.3 and Gradle projects. That "Could not find or load main class 1.8" error is almost always tied to a misconfiguration in how IDEA is handling Java versions for your builds. Here are targeted troubleshooting steps to track down the problem:
1. Verify Project-Level SDK Configuration
First, check that your project is pointing to a valid JDK instead of treating "1.8" like a class name:
- Open
File → Project Structure → Project Settings → Project - Confirm the Project SDK is set to a proper Java 1.8 JDK (not just the text "1.8")
- Double-check the Project language level matches the
sourceCompatibility/targetCompatibilitydefined in your Gradle script
2. Check Module-Specific SDK Settings
Sometimes module-level configs override project settings:
- Go to
Project Structure → Modules → [Your Module] → Dependencies - Ensure the Module SDK matches your project's JDK, and there are no invalid entries listed
3. Audit Gradle Run Configurations
IDEA's run configurations might have corrupted parameters:
- Open
Run → Edit Configurations - Find your Gradle-based application run config
- Look for odd values in VM options or Environment variables—it’s easy to accidentally mistype a flag that ends up treating "1.8" as a main class
- Confirm the Use classpath of module option points to the correct module containing your main class
4. Refresh and Reimport Gradle Project
IDEA’s cached configs can get out of sync:
- In the right-side Gradle panel, click the "Refresh all Gradle projects" icon (the circular arrow)
- If that doesn’t help, try deleting the
.ideafolder and all.imlfiles, then reimport the project from scratch (backup any custom IDE settings first!)
5. Check IDEA’s Gradle JVM Setting
IDEA might be using the wrong JVM to execute Gradle tasks:
- Open
File → Settings → Build, Execution, Deployment → Build Tools → Gradle - Look at the Gradle JVM dropdown—make sure it’s set to your Java 1.8 JDK, not IDEA’s internal JRE or an invalid path
6. Validate Gradle Script Compatibility Settings
Double-check your build.gradle to ensure version settings are formatted correctly:
sourceCompatibility = 1.8 targetCompatibility = 1.8
Avoid wrapping the version in quotes (e.g., '1.8')—while Gradle accepts it, it can sometimes confuse IDEA’s configuration parser.
Why Gradle Wrapper Works
The Gradle wrapper uses its own configured environment: it relies on the JDK specified in your system’s JAVA_HOME or the Gradle distribution’s default settings, bypassing IDEA’s potentially misconfigured settings. That’s why it runs smoothly even when IDEA doesn’t.
内容的提问来源于stack exchange,提问作者Igor

