【问题标题】:unable to find logger class while running a maven build jar file运行 Maven 构建 jar 文件时找不到记录器类
【发布时间】:2017-08-12 13:16:26
【问题描述】:

我正在尝试编写一个 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>

【问题讨论】:

  • maven integrated API 是什么意思?您生成的 JAR 是插件、库还是应用程序?对于库和插件,运行时和编译范围依赖项会自动包含在内,对于应用程序,您需要嵌入它们或自己提供它们。对于Java -jar,您通常必须全部嵌入。

标签: java maven logging log4j


【解决方案1】:

您的 pom.xml 中的 &lt;scope&gt; 似乎是错误的。试试这个(注意我已经把“test”改成了“compile”)。

<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”是默认范围,因此如果您关闭 &lt;scope&gt; 元素,则范围将是“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 “范围”look here 的更多信息。

【讨论】:

    【解决方案2】:

    这是一个类路径问题,Maven 生成的 jar 只包含你的类。要解决此问题,您可以将所有依赖项打包到项目 jar 中:How can I create an executable JAR with dependencies using Maven?

    【讨论】:

      【解决方案3】:

      如果你想创建一个包含所有依赖 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
      

      并在名为weather_simulator-1.0.0-SNAPSHOT-jar-with-dependencies.jartarget 目录中找到包含依赖项的生成jar 文件,您应该使用它来调用您的应用程序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-28
        • 1970-01-01
        • 1970-01-01
        • 2015-11-16
        • 2018-08-23
        • 2020-10-04
        • 2016-05-21
        • 2014-12-14
        相关资源
        最近更新 更多