下面是一个用maven构建的使用SL4J的简单Java代码。以下命令没有问题。它们都运行良好,mvn编译mvn包mvn干净安装mvn exec:java
但是当我尝试以下任何一种
java -cp target/MaventestApp-1.0-SNAPSHOT.jar com.maven.helloworld.App
jar tf ./target/MaventestApp-1.0-SNAPSHOT.jar我得到以下错误
exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.maven.helloworld.App.main(App.java:17)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)下面是我的pom.xml文件的相关部分:
<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>
<slf4jVersion>1.6.1</slf4jVersion>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.maven.helloworld.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.maven.helloworld.App</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>此示例的代码为这里
有人能帮我更好地理解maven和执行过程吗?感谢你的时间。
发布于 2018-06-26 13:28:19
问题在于如何构建JAR文件(不包括依赖项)。您可以使用maven-程序集-插件 (参见程序集和jar插件这里之间的区别)。
基本上,将当前构建部分替换为以下内容:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.maven.helloworld.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>然后使用以下命令执行生成的jar与-依赖关系:
java -jar target/MaventestApp-1.0-SNAPSHOT-jar-with-dependencies.jar请参阅有关创建可执行JAR 这里的更多信息。
发布于 2018-06-26 11:04:52
应该由Exec插件来完成这项工作。有点像
mvn exec:java -Dexec.mainClass=com.maven.helloworld.App实际的问题是,您只将jar添加到类路径中,并且由于您自己的存档中没有sl4j类,而是将它们声明为外部依赖项,因此它们很简单地丢失(又名NoClassDefFoundError)。
上面的命令将所有运行时依赖项链接到pom中配置的类路径。
https://stackoverflow.com/questions/51041229
复制相似问题