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

Maven exec插件无法找到mainClass的问题咨询与解决请求

Maven exec:java 报 ClassNotFoundException: src.com.statsbot.Bot 问题排查

我在构建Java Maven项目时遇到问题:mvn compile执行正常,但运行mvn exec:java时始终抛出java.lang.ClassNotFoundException: src.com.statsbot.Bot错误。

报错日志

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< MK_Degens:32er_Bot >-------------------------
[INFO] Building 32er_Bot 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ 32er_Bot ---
[WARNING] 
java.lang.ClassNotFoundException: src.com.statsbot.Bot
    at java.net.URLClassLoader.findClass (URLClassLoader.java:445)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:587)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:520)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:270)
    at java.lang.Thread.run (Thread.java:833)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.549 s
[INFO] Finished at: 2023-05-09T13:51:13+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project 32er_Bot: An exception occured while executing the Java class. src.com.statsbot.Bot -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

当前pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>MK_Degens</groupId>
    <artifactId>32er_Bot</artifactId>
    <version>1.0</version>

    <name>32er_Bot</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots</artifactId>
            <version>6.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>src.com.statsbot.Bot</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

项目情况

我的.java文件位于src目录下,第一行是package com.statsbot;,类名为Bot。已经尝试过mvn exec:exec、mvn clean install,以及调整mainClass的各种配置,问题仍未解决。


解决方案

1. 修正mainClass配置

Maven的mainClass需要填写完整的类全名(包名+类名),不需要加上src前缀。你的类包名是com.statsbot,类名是Bot,所以正确的配置应该是:

<configuration>
    <mainClass>com.statsbot.Bot</mainClass>
</configuration>

2. 对齐Maven源码目录规范

Maven默认的源码目录是src/main/java,如果你的.java文件直接放在src目录下,Maven编译时不会自动识别这些文件。你需要调整目录结构:

  • 创建src/main/java/com/statsbot目录
  • 将你的Bot.java移动到src/main/java/com/statsbot/路径下

3. 重新编译运行

修改完成后,执行以下命令:

mvn clean compile exec:java

这样Maven就能正确找到编译后的类文件并执行了。


内容的提问来源于stack exchange,提问作者chef

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 10:47:48