簡體   English   中英

無法初始化主類,導致: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“編譯”、“安裝”命令以創建可執行 JAR 並繼續運行該 JAR 后,我收到以下錯誤

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

我不知道發生了什么。 我該如何解決這個問題? 下面是一些進一步的細節。 我使用主類信息更新了 POM,並將類路徑設置為“true”。

主要類別代碼

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 例外等級代碼

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 在那之后工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM