2017-04-09 124 views
0

我有一個使用三個庫的Maven項目(Netbeans IDE):gson,okhttp,okio。我想建設項目,通過終端和命令$ Java的罐子app.jar後執行與創建了.jar,但我不斷收到以下錯誤:如何運行-jar文件(java.lang.NoClassDefFoundError)

Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/Callback 
    at it.bitrack.main.Main.main(Main.java:43) 
Caused by: java.lang.ClassNotFoundException: okhttp3.Callback 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 1 more 

我已經添加了.jar文件這三個庫與app.jar位於同一個文件夾中,並且位於名爲libs的子文件夾中,但沒有任何積極影響。

你能猜出爲什麼以及我可以嘗試使它工作嗎?在NetBeans中執行的應用程序運行完全...

這是我的POM文件:

<?xml version="1.0" encoding="UTF-8"?> 
<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>it.bitrack</groupId> 
    <artifactId>BiTrackAssetSimulator</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 

    <dependencies> 

     <dependency> 
      <groupId>com.google.code.gson</groupId> 
      <artifactId>gson</artifactId> 
      <version>2.8.0</version> 
     </dependency> 

     <dependency> 
      <groupId>com.squareup.okhttp3</groupId> 
      <artifactId>okhttp</artifactId> 
      <version>3.6.0</version> 
     </dependency> 

    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <!-- Build an executable JAR --> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-jar-plugin</artifactId> 
       <version>3.0.2</version> 
       <configuration> 
        <archive> 
         <manifest> 
          <addClasspath>true</addClasspath> 
          <classpathPrefix>lib/</classpathPrefix> 
          <mainClass>it.bitrack.main.Main</mainClass> 
         </manifest> 
        </archive> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

謝謝。

回答

2

您必須在您的JAR中嵌入依賴關係。

使用Maven Dependency插件這樣做:https://maven.apache.org/plugins/maven-assembly-plugin/usage.html

例子:

<plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <configuration> 
    <archive> 
     <manifest> 
     <mainClass>it.bitrack.main.Main</mainClass> 
     </manifest> 
    </archive> 
    <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> 

然後:mvn package

+0

真棒它的工作!非常感謝! – user1060551