IntelliJ运行项目遇Could not find or load main class问题及类继承疑问
Configured/Tool Breaks Main Class Loading in IntelliJ (And Fixes) First, let's cut to the chase: The "Could not find or load main class" error here isn't actually that your WordCount or HelloWorld class is missing—it's that the JVM can't load its parent classes (Configured and Tool), which causes the entire main class to fail initialization. IntelliJ's default Maven integration settings often trip up here for a few key reasons:
Common Causes From IntelliJ's Perspective
- Incorrect Maven Dependency Scope: If the library containing
Configured/Tool(like Hadoop Common or Apache Commons CLI) is marked with<scope>provided</scope>in yourpom.xml, IntelliJ excludes these JARs from the runtime classpath by default. Theprovidedscope tells Maven the dependency will be supplied by a production container (like a Hadoop cluster), but when running locally, you don't have that container—so the parent classes are missing. - Out-of-Sync Maven Dependencies: Sometimes IntelliJ doesn't automatically sync new or updated Maven dependencies. Even if your
pom.xmlis correct, the IDE might not have downloaded the JARs or added them to your module's classpath. - Misconfigured Run Configuration: The default run configuration for your main class might only include your project's compiled
.classfiles, not the Maven dependency JARs. Without those JARs, the JVM can't resolve the parent classes needed to load your main class.
Step-by-Step Fixes
Let's walk through the most reliable solutions:
1. Fix Maven Dependency Scope
Open your pom.xml and locate the dependency for Configured/Tool (e.g., org.apache.hadoop:hadoop-common):
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>your-version</version> <!-- Change this from 'provided' to 'compile' for local testing --> <scope>compile</scope> </dependency>
If you need provided for production, you can keep it but adjust your run configuration (see step 3) to include provided dependencies at runtime.
2. Force Maven Dependency Sync
- In IntelliJ's right-hand Maven panel, click the Reload All Maven Projects button (the circular arrow icon).
- Alternatively, right-click your project in the Project view → Maven → Reload Project.
Wait for the sync to finish—this ensures IntelliJ downloads all required JARs and adds them to your classpath.
3. Correct Your Run Configuration
- Open Run → Edit Configurations.
- Select your main class's run configuration from the list.
- Switch to the Classpath tab:
- Check if your Maven dependencies are listed under Dependencies. If not, click Add → Library → Select your project's Maven library (e.g.,
Maven: org.apache.hadoop:hadoop-common:...) and add it. - If you kept the
providedscope, look for the Include dependencies with "Provided" scope checkbox and enable it (this tells IntelliJ to include those JARs in the runtime classpath).
- Check if your Maven dependencies are listed under Dependencies. If not, click Add → Library → Select your project's Maven library (e.g.,
- Click Apply then OK, and re-run your main class.
4. Verify Module Library Settings
- Right-click your project → Open Module Settings (or press
F4). - Go to Libraries in the left sidebar.
- Ensure all your Maven dependencies are listed here. If any are missing, click the + icon → From Maven and search for the missing dependency to add it manually.
Quick Test
After applying these fixes, run your HelloWorld class that inherits Configured/Tool. If the parent classes are now on the classpath, the JVM will successfully load your main class, and the error should disappear.
内容的提问来源于stack exchange,提问作者Dims

