英文:
Unable to initialize main class, Caused by: java.lang.NoClassDefFoundError: com/mashape/unirest/http/exceptions/UnirestException [JAVA]
问题
问题
在Eclipse中运行Maven的compile和install命令以创建可执行的JAR文件,并尝试运行该JAR文件后,我收到以下错误信息:
Error: 无法初始化主类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文件
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
英文:
PROBLEM
After I run the Maven 'compile', 'install' command in Eclipse to create an executable JAR and proceed to run that JAR I get the below error
Error: Unable to initialize main class org.example.project.StockTracker`
Caused by: java.lang.NoClassDefFoundError: com/mashape/unirest/http/exceptions/UnirestException
I have no clue what is going on. How can I resolve this? Below are some further details. I updated the POM with the main-class information and set the classpath to 'true'.
MAIN CLASS CODE
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 EXCEPTION CLASS CODE
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);
	}
}
MANIFEST-FILE IN 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
答案1
得分: 4
通过 @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 文件可以正常工作。
英文:
With the help of @IlyaSenko I managed to figure it out. I needed to include all my JAR dependencies within my actual JAR as opposed to simply declaring the dependency within my POM file. I updated my POM with the below to achieve this..
<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>
Then executed the following command using Maven "mvn clean compile assembly:single" which bundled all the JARs into one executable JAR. JAR worked after that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论