0

I have a maven project that uses the json-simple library. When I create jar file using

mvn clean
mvn test
mvn install

and try to run the jar file using java -jar target/Game2048-1.0-SNAPSHOT.jar I get an error as shown below:

enter image description here

My pom.xml file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Game2048_GId</groupId>
<artifactId>Game2048</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

Is there anything wrong with my pom file? There are multiple classes for the project (but just 1 main class)

Andreas
  • 2,455
  • 10
  • 21
  • 24
Chetan
  • 469
  • 3
  • 12
  • 27

2 Answers2

0

As per the screenshot it seems you are getting ClassNotFoundException, which is mainly due to scope,scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

Try Adding scope to json-simple dependency.

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
    <scope>provided</scope>
</dependency>

I think the problem is with scope , please try changing the scope to "compile" or "test" after provided in case if it dont work still.

FYI

What is <scope> under <dependency> in pom.xml for?


Edit: Change provided to compile.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
0

try with java -cp {absolute path of your m2 dir or jar library} -jar {your jar}.jar. The reason for no class Def found error is always that jar is not part of your classpath , -cp option will add the json simple jar and its dependencies to the classpath and your error would be solved .

satyesht
  • 499
  • 7
  • 19
  • not always, why to hardcode stuffs when you have maven?? – Vishwa Ratna Mar 13 '19 at 06:38
  • What is the other reason for noclassdeferror other than class not present on classpath or inaccessible by jvm?? And what is the hardcoding here !! – satyesht Mar 13 '19 at 06:41
  • In real application projects it could happen that dependency jar that you have to consume is still under development and you have only interface jar for it , in that case maven and nexus repos wont be able to provide you the implementation jar , that is why maven scope provided would be used , and in production you can give the impl jar by -cp option .Agree ideally depenedency management should be done via maven but it is not always best fit for all problems . – satyesht Mar 13 '19 at 06:57