Maven - 没有找到类异常

时间:2015-02-15 13:20:26

标签: maven

我正在尝试构建我的maven项目,但是我遇到以下异常:

java.lang.ClassNotFoundException: com.example.message.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:281)
at java.lang.Thread.run(Thread.java:745)

我的POM文件如下。我确保包名称包含在<mainClass>标记中。但它仍然无法找到班级?

<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>com.example</groupId>
  <artifactId>message</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <mainClass>com.example.message.Main</mainClass>
      <arguments>
        <argument>AddTrial</argument>
      </arguments>
    </configuration>
  </plugin>
</plugins>
  </build>
  <dependencies>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.8.Final</version>
</dependency>
<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901-1.jdbc4</version>
</dependency>
  </dependencies>
</project>

1 个答案:

答案 0 :(得分:3)

您需要确保在运行任何依赖于已编译类的plugin goals之前构建Maven项目(例如exec:java)。

在这种情况下,它应该像运行

一样简单
mvn install exec:java

或IDE中的等效目标。