命令行运行Maven项目遇MojoExecutionException:类找不到问题咨询
Hey there, let's work through this issue you're facing with mvn exec:java! The MojoException and underlying ClassNotFoundException are happening for two key reasons: either your code hasn't been properly compiled, or Maven's exec plugin doesn't have clear instructions on where to find your main class (right now, your pom.xml doesn't include the exec plugin configuration at all).
Let's break down the solutions step by step:
1. Quick Fix: Compile First + Verify Command Line Arguments
Before running the exec command, make sure your code is compiled so Maven can access the .class file:
- First, run this to compile your project:
This will generate your compiled class files in themvn clean compiletarget/classesdirectory. Double-check thattarget/classes/com/example/Main.classexists here—if it doesn't, your class path or package structure is wrong (make sure your.javafile is insrc/main/java/com/example/). - Then re-run your original command (confirm the full class name is correct, no typos!):
mvn -e exec:java -Dexec.mainClass="com.example.Main"
2. Recommended Long-Term Fix: Configure the Exec-Maven-Plugin in pom.xml
Adding the plugin to your pom.xml makes running your main class easier (no need to specify the main class every time) and avoids classpath confusion. Here's how to update your pom:
<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>Linux</groupId> <artifactId>Linux</artifactId> <version>1.0</version> <!-- Add this build section to configure the exec plugin --> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <!-- Use the latest stable version --> <configuration> <!-- This is where you specify your main class's full path --> <mainClass>com.example.Main</mainClass> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.37.1</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <!-- Needs to be the same version that REST Assured depends on --> <version>2.1.2</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> </dependency> </dependencies> </project>
Once you've updated the pom, running your main class is as simple as:
mvn exec:java
Quick Checks to Rule Out Other Issues
- Package Structure: Ensure your
Main.javafile is located insrc/main/java/com/example/(Maven uses this as the default source directory for non-test code). - Class Name Spelling: Double-check that the full class name
com.example.Mainmatches exactly what's in your code (case-sensitive!). - Dependency Conflicts: If you still run into issues, verify that none of your dependencies are conflicting with the exec plugin (though this is unlikely with the version we recommended).
内容的提问来源于stack exchange,提问作者Eddi

