Exception in thread "main" java.lang.NoClassDefFoundError when compiling maven project

60 Views Asked by At

I'm trying to make a simple currency converter program in java with maven and am using json data so I try to import the "org.json.simple" dependency and have the dependency tag in my pom.xml. However, when I compile I get "Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser." my pom.xml file:



  <dependencies>
    <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
    </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.6.0</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.4.1</version>
          <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>com.myapp.app.App</mainClass>
                </manifest>
              </archive>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

the code that I am trying to run:

import org.json.simple.*;
import org.json.simple.parser.*;

public class Api {
public static String getFinalAmount() {
    String finalAmount = "";
    JSONParser parser = new JSONParser();
    try {
        Object obj = parser.parse(new FileReader("/Users/User/Desktop/course.json"));
        JSONObject jsonObject = (JSONObject)obj;
        finalAmount = (String)jsonObject.get("new_amount");
        return finalAmount;

    } catch (Exception e) {
      e.printStackTrace();
    }
    return finalAmount;
  }
}

I have already tried adding the maven assembly plugin and the dependency plugin with neither helping. I'm not sure what to try next since I don't usually use Java or maven and I haven't been able to find much on this topic. Thank you in advance.

1

There are 1 best solutions below

2
SIVA K. On

Code and json-simple Dependecy are looking fine. Check if you were able to see json-simple-1.1.1.jar file in your classpath if not try adding it. you should be able to get JSONParser form org.json.simple.*