运行 Maven 构建 JAR 文件时找不到记录器类



我正在尝试编写一个maven集成的Java API。我已经包含了log4j用于日志记录目的。这在通过 eclipse 运行时效果很好,但是当 maven 包完成并且 jar 运行时,它无法使用 java -jar 从 cmd 行运行jar_name.jar引发错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger

现在 log4j.properties 文件放在 src/main/resources 文件夹下。提到了绒球.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中的<scope>.xml似乎是错误的。试试这个(请注意,我已将"test"更改为"编译"(。

<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测试"时提供log4j jar。如果您在编译时和运行时都需要 jar(这是似乎给您带来问题的场景(,则范围需要"编译"。

请注意,"编译"是默认范围,因此如果关闭<scope>元素,则范围将为"编译"。

来自 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

并在名为 likeweather_simulator-1.0.0-SNAPSHOT-jar-with-dependencies.jartarget目录中找到生成的 jar 文件,该文件应该用于调用应用。

最新更新