我必须在cmd中运行maven项目。这是我的pom.xml文件:
http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<!-- FIXME change it to the project's website -->
<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>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
该项目在IDE(Eclipse)中运行正常,但是当我尝试运行该项目时,从命令行运行它,我得到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at com.mycompany.app.App.main(App.java:15)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
答案 0 :(得分:1)
您遇到的问题是您在类路径中没有依赖项。您可以使用Maven插入来获得这种效果。
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
来源How can I create an executable JAR with dependencies using Maven?
答案 1 :(得分:1)
这实际上取决于您如何设置项目,但是堆栈跟踪实际上是在说JVM无法找到所提到的类。现在,您可以想象(这只是许多可能的问题之一)-您的代码在某处引用了“ Configuration”类,但是当项目从命令行运行时-JVM无法找到它。
我建议您看一下this主题以了解收到的错误。提出了许多想法,并且解释得很好。
基本上,一旦您有了所有依赖项,该错误就会消失,并且如果您使用的是TestNG或JUnit,则不需要主类。如果将mvn clean test
添加到POM,则只需mvn
或更短的<defaultGoal>clean test</defaultGoal>
运行测试。