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

IntelliJ运行项目遇Could not find or load main class问题及类继承疑问

Why Inheriting 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 your pom.xml, IntelliJ excludes these JARs from the runtime classpath by default. The provided scope 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.xml is 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 .class files, 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

  1. Open Run → Edit Configurations.
  2. Select your main class's run configuration from the list.
  3. 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 provided scope, look for the Include dependencies with "Provided" scope checkbox and enable it (this tells IntelliJ to include those JARs in the runtime classpath).
  4. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:43:12