javaeclipsemavennoclassdeffounderrorunirest

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


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

Solution

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