【问题标题】:Unable to initialize main class, Caused by: java.lang.NoClassDefFoundError: com/mashape/unirest/http/exceptions/UnirestException [JAVA]无法初始化主类,原因是:java.lang.NoClassDefFoundError: com/mashape/unirest/http/exceptions/UnirestException [JAVA]
【发布时间】:2020-06-27 22:17:56
【问题描述】:

问题

在 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

我不知道发生了什么。我该如何解决这个问题?下面是一些进一步的细节。我用主类信息更新了 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

【问题讨论】:

  • 您的 jar 中是否包含 UnirestException 的依赖项?
  • 这里是路径..com.mashape.unirest.http.exceptions。这个特定的异常类嵌入在我称之为 POM 中的依赖项的唯一 jar 中。实际的 unirest JAR 可执行文件本身不在我的 JAR 中,而是在我的清单文件中贴花......
  • @IlyaLysenko 好的,我想我已经部​​分解决了。我将物理 JAR 依赖项复制到包含我的 JAR 可执行文件的“目标”文件夹中。像这样运行程序有效。但是,有没有办法可以将这些依赖项嵌入到我的 JAR 文件中以避免这样做?有 maven 命令吗?
  • mvn package 命令必须组装一个带有依赖关系的 jar。
  • 谢谢@IlyaLysenko!我使用了 mvn clean compile assembly:single 并且成功了!只需要更新 POM 文件,所有依赖项都插入到 1 个 JAR 中。

标签: java eclipse maven noclassdeffounderror unirest


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2021-11-04
    • 2021-02-21
    • 2012-10-10
    相关资源
    最近更新 更多