2017-05-16 56 views
2

嘗試使用maven-assembly插件創建一個包含所有依賴項的胖jar文件。嘗試了一堆東西,但最終與主類未找到錯誤。好像我碰到了一堵牆,現在在這裏待了幾個小時。運行由maven-assembly創建的可執行文件jar:錯誤:無法找到或加載主類

pom.xml片段:

<project> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>foobar</groupId> 
    <artifactId>foobar</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <outputDirectory>/home/jars/foobar/</outputDirectory> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
        <archive> 
         <manifest> 
          <mainClass>com.foobar.Main</mainClass> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

    <dependencies>...</dependencies> 
</project> 

Maven的命令執行:

mvn clean compile assembly:single

mvn package assembly:single

mvn clean compile package assembly:single

創建在/home/jars/foobar/foobar-1.0-SNAPSHOT-jar-with-dependencies.jar罐子如預期

MANIFEST.INFMETA-INF

Manifest-Version: 1.0 
Archiver-Version: Plexus Archiver 
Built-By: user 
Created-By: Apache Maven 3.3.9 
Build-Jdk: 1.8.0_131 
Main-Class: com.foobar.Main 

爪哇命令執行:

java -jar foobar-1.0-SNAPSHOT-jar-with-dependencies.jar

java -cp foobar-1.0-SNAPSHOT-jar-with-dependencies.jar com.foobar.Main

java -jar foobar-1.0-SNAPSHOT-jar-with-dependencies.jar -cp com.foobar.Main

全部返回相同的錯誤:Error: Could not find or load main class com.foobar.Main

OS:Fedora 25

java -version 
openjdk version "1.8.0_131" 
OpenJDK Runtime Environment (build 1.8.0_131-b12) 
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode 

javac -version 
javac 1.8.0_121 

mvn --version 
Apache Maven 3.3.9 (NON-CANONICAL_2016-07-01T11:53:38Z_mockbuild; 2016-07-01T17:23:38+05:30) 
Maven home: /usr/share/maven 
Java version: 1.8.0_131, vendor: Oracle Corporation 
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-1.b12.fc25.x86_64/jre 
Default locale: en_US, platform encoding: UTF-8 
OS name: "linux", version: "4.10.13-200.fc25.x86_64", arch: "amd64", family: "unix" 

解決:

的問題是,源代碼沒有得到編譯。無法在胖罐中找到源代碼jar。看起來像maven認爲一定的包裝結構。

加入以下<build>解決這個問題:

<sourceDirectory>src/com/foobar</sourceDirectory> 
+1

試着看看.jar​​裏面,看看Maven如何整理文件?你能看到你的班級嗎?我爲Spring引導應用程序構建了類似的問題,其中插件已將文件移動到兼容Spring引導的文件中,但無法使用「java -jar ...」運行 –

+0

結果如果您嘗試使用' maven install'? –

+0

@MinhKieu就是這樣!嘆!似乎maven假定某個目錄結構。通過添加''來解決它。如果你能回答這個問題,我會將它標記爲正確的。 – user2354302

回答

1

的Maven插件並重組了JAR的春季啓動兼容這可能會導致命令行「Java的罐子......」沒有內容好好工作。當用戶user2354302發現,添加src/com/foobar幫助。

相關問題