Unable to initialize main class, Caused by: java.lang.NoClassDefFoundError: com/mashape/unirest/http/exceptions/UnirestException [JAVA]

Unable to initialize main class, Caused by: java.lang.NoClassDefFoundError: com/mashape/unirest/http/exceptions/UnirestException [JAVA]

问题

在我 运行 在 Eclipse 中使用 Maven 'compile'、'install' 命令创建一个可执行 JAR 并继续 运行 该 JAR 我得到以下错误

Error: Unable to initialize main class org.example.project.StockTracker` Caused by: java.lang.NoClassDefFoundError: com/mashape/unirest/http/exceptions/UnirestException

我不知道发生了什么。我该如何解决这个问题?下面是一些进一步的细节。我用 main-class 信息更新了 POM,并将 class 路径设置为 'true'。

主要CLASS代码

package org.example.project;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

public class StockTracker {

    public static void main (String[] args) throws UnirestException, InterruptedException  {
        HttpResponse<String> response = Unirest.get("https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/get-quotes?region=US&lang=en&symbols=TSLA")
                    .header("x-rapidapi-host", "apidojo-yahoo-finance-v1.p.rapidapi.com")
                    .header("x-rapidapi-key", "api-key")
                    .asString();

        //System.out.println(response.getBody());
        System.out.println("response.getBody()");
    }
}

UNIREST 异常 CLASS 代码

package com.mashape.unirest.http.exceptions;

public class UnirestException extends Exception {

    private static final long serialVersionUID = -3714840499934575734L;

    public UnirestException(Exception e) {
        super(e);
    }

    public UnirestException(String msg) {
        super(msg);
    }

}

JAR 中的清单文件

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: Author
Build-Jdk: 13.0.1
Class-Path: unirest-java-1.4.9.jar httpclient-4.5.2.jar httpcore-4.4.4.j
 ar commons-logging-1.2.jar commons-codec-1.9.jar httpasyncclient-4.1.1.
 jar httpcore-nio-4.4.4.jar httpmime-4.5.2.jar json-20160212.jar
Main-Class: org.example.project.StockTracker

在@IlyaSenko 的帮助下,我设法弄明白了。我需要在我的实际 JAR 中包含我所有的 JAR 依赖项,而不是简单地在我的 POM 文件中声明依赖项。我用下面的内容更新了我的 POM 来实现这个..

<plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>org.example.project.StockTracker</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
</plugin>

然后使用 Maven“mvn clean compile assembly:single”执行以下命令,将所有 JAR 捆绑到一个可执行 JAR 中。 JAR在那之后工作。