简体   繁体   English

运行Maven构建jar文件时无法找到记录器类

[英]unable to find logger class while running a maven build jar file

I am trying to write a maven integrated Java API. 我正在尝试编写一个Maven集成Java API。 I have included log4j for logging purpose. 我已包括log4j用于记录目的。 Which works well when running through eclipse, but when maven package is done and the jar is run it is unable to run from cmd line using java -jar jar_name.jar throwing an error 在eclipse中运行时效果很好,但是当完成maven程序包并运行jar时,无法使用java -jar jar_name.jar从cmd行运行,并抛出错误

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

Now the log4j.properties file is placed under src/main/resources folder. 现在,将log4j.properties文件放置在src / main / resources文件夹下。 And the pom.xml is mentioned. 并提到了pom.xml。 Have tried searching for answers but none worked for me. 曾尝试寻找答案,但没有一个对我有用。 Any help available 任何可用的帮助

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

Your <scope> in your pom.xml appears to be wrong. 您pom.xml中的<scope>似乎是错误的。 Try this (notice I've changed "test" to "compile"). 试试这个(注意,我已经将“测试”更改为“编译”)。

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.6.1</version>
    <scope>compile</scope>    <!-- look here -->
</dependency>

The way you currently have your pom.xml configured (with "test"), maven will only provide the log4j jar when doing "mvn test". 当前配置pom.xml的方式(带有“ test”),maven仅在执行“ mvn test”时才提供log4j jar。 If you need the jar at both compile time and run time (which is the scenario that appears to be causing problems for you), the scope needs to be "compile". 如果在编译时和运行时都需要jar(这似乎会给您造成问题),则范围必须是“编译”的。

Note that "compile" is the default scope, so if you leave the <scope> element off, the scope will be "compile". 请注意,“ compile”是默认作用域,因此,如果禁用<scope>元素,则作用域将是“ compile”。

From the maven docs: "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文档: "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"

For more info about maven "scopes" look here . 有关Maven“范围”的更多信息, 请点击此处

This is a classpath problem, the jar generated by Maven contains only your classes. 这是一个类路径问题,由Maven生成的jar仅包含您的类。 To fix this you can pack all the dependencies inside your project jar: How can I create an executable JAR with dependencies using Maven? 要解决此问题,您可以将所有依赖项打包到项目jar中: 如何使用Maven创建具有依赖项的可执行JAR?

There are two things you should be aware if you like to create an executable jar which contains all dependent jars you have to use maven-assembly-plugin as you already did but you forgot to bind it to the life cycle...which looks like this: 如果要创建一个包含所有依赖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>

Furthermore having plugins as dependencies is simply wrong.. 此外,将插件作为依赖项完全是错误的。

<dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.6.1</version>
</dependency>

This means remove this entry from your pom file. 这意味着从您的pom文件中删除该条目。

Defining a dependency with a scope test means it will be available only during the unit tests which means also it will never being packaged into a resulting jar file. 使用范围test定义依赖项意味着它仅在单元测试期间可用,这也意味着它永远不会打包到生成的jar文件中。 This means you have to change the following: 这意味着您必须更改以下内容:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.6.1</version>
    <scope>test</scope>
</dependency>

into the following: 分为以下内容:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.6.1</version>
</dependency>

After you have fixed those issues you should be able to build your app via: 解决这些问题后,您应该可以通过以下方式构建应用程序:

mvn clean package

and find the resulting jar file which contains the dependencies in the target directory named like weather_simulator-1.0.0-SNAPSHOT-jar-with-dependencies.jar which you should use to call your app. 并在target目录中找到包含依赖项的生成的jar文件,该文件名为weather_simulator-1.0.0-SNAPSHOT-jar-with-dependencies.jar ,您应使用它来调用应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM