如何运行 -jar 文件 (java.lang.NoClassDefFoundError)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43309323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:19:43  来源:igfitidea点击:

How to run -jar file (java.lang.NoClassDefFoundError)

javamavennetbeansjarnoclassdeffounderror

提问by user1060551

I have a Maven project (Netbeans IDE) that uses three libraries: gson, okhttp, okio. I am trying to execute the .jar created with after building the project, through Terminal and the command $java -jar app.jar, but I keep getting the error below:

我有一个 Maven 项目(Netbeans IDE),它使用三个库:gson、okhttp、okio。我正在尝试通过终端和命令 $java -jar app.jar 执行构建项目后创建的 .jar,但我不断收到以下错误:

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

I already added the .jar files of these three libraries in the same folder as the app.jar and in a subfolder called libs, however no positive effect.

我已经将这三个库的 .jar 文件添加到与 app.jar 相同的文件夹和名为 libs 的子文件夹中,但是没有任何积极效果。

Would you be able to guess why and what I could try to make it work? The app runs perfectly when executed within Netbeans...

你能猜出为什么以及我可以尝试让它发挥作用吗?该应用程序在 Netbeans 中执行时运行完美......

This is my POM file:

这是我的 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>

Thank you.

谢谢你。

回答by cactuschibre

You have to embed dependencies in your JAR.

您必须在 JAR 中嵌入依赖项。

Use the Maven Dependency Plugin to do so : https://maven.apache.org/plugins/maven-assembly-plugin/usage.html

使用 Maven 依赖插件来做到这一点:https: //maven.apache.org/plugins/maven-assembly-plugin/usage.html

Example :

例子 :

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

Then: mvn package

然后: mvn package