2017-08-12 46 views
1

我想寫一個maven集成的Java API。爲了記錄目的,我已經包含了log4j。通過Eclipse中運行時效果很好,但在行家包完,罐子時,它會無法使用Java的罐子jar_name.jar拋出一個錯誤找不到記錄器類,同時運行一個Maven構建jar文件

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

現在log4j.properties文件從CMD線運行放在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> 

+0

'maven集成API'是什麼意思?你的JAR是一個插件,一個庫或一個應用程序?對於庫和插件,運行時和編譯範圍相關性會自動包含在內,對於需要嵌入或自行提供的應用程序。在'Java -jar'的情況下,你通常需要嵌入所有的東西。 – eckes

回答

1

幫助你<scope>在你的pom.xml似乎是錯誤的。試試這個(注意我已經把「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(這是看起來對您造成問題的場景),則範圍需要「編譯」。

注意,「編譯」是默認的範圍,因此,如果你離開<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"

有關行家 「範圍」 look here更多信息。

0

有兩件事情,如果你想創建一個包含您必須使用Maven的組裝插件,因爲所有相關的JAR一個可執行的JAR,你應該知道你已經做了,但你忘了將其綁定到生命週期......這看起來是這樣的:

<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.jar的名爲target的目錄中包含依賴關係的jar文件,該目錄應該用於調用您的應用程序。

相關問題