[英]unable to find logger class while running a maven build jar file
我正在尝试编写一个Maven集成Java API。 我已包括log4j用于记录目的。 在eclipse中运行时效果很好,但是当完成maven程序包并运行jar时,无法使用java -jar jar_name.jar从cmd行运行,并抛出错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
现在,将log4j.properties文件放置在src / main / resources文件夹下。 并提到了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>Weather_Simulator</groupId>
<artifactId>weather_simulator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>weather_simulator</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.test.weather.simulator.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<!-- NOTE: We don't need a groupId specification because the group is
org.apache.maven.plugins ...which is assumed by default.
-->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
您pom.xml中的<scope>
似乎是错误的。 试试这个(注意,我已经将“测试”更改为“编译”)。
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.6.1</version>
<scope>compile</scope> <!-- look here -->
</dependency>
当前配置pom.xml的方式(带有“ test”),maven仅在执行“ mvn test”时才提供log4j jar。 如果在编译时和运行时都需要jar(这似乎会给您造成问题),则范围必须是“编译”的。
请注意,“ compile”是默认作用域,因此,如果禁用<scope>
元素,则作用域将是“ compile”。
来自maven文档: "This [compile] is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects"
有关Maven“范围”的更多信息, 请点击此处 。
这是一个类路径问题,由Maven生成的jar仅包含您的类。 要解决此问题,您可以将所有依赖项打包到项目jar中: 如何使用Maven创建具有依赖项的可执行JAR?
如果要创建一个包含所有依赖jar的可执行jar,则需要注意两件事,就像以前一样,必须使用maven-assembly-plugin,但是忘记将其绑定到生命周期中……这个:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<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>
[...]
</project>
此外,将插件作为依赖项完全是错误的。
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</dependency>
这意味着从您的pom文件中删除该条目。
使用范围test
定义依赖项意味着它仅在单元测试期间可用,这也意味着它永远不会打包到生成的jar文件中。 这意味着您必须更改以下内容:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.6.1</version>
<scope>test</scope>
</dependency>
分为以下内容:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.6.1</version>
</dependency>
解决这些问题后,您应该可以通过以下方式构建应用程序:
mvn clean package
并在target
目录中找到包含依赖项的生成的jar文件,该文件名为weather_simulator-1.0.0-SNAPSHOT-jar-with-dependencies.jar
,您应使用它来调用应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.