簡體   English   中英

運行Maven構建jar文件時無法找到記錄器類

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM