0

So i have this dependency

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180130</version>
</dependency>

and after

mvn package

when i run jar i get this error

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject

when i run my app in intellij everything works perfectly, the problem is that maven is not including libs from dependencies into final jar.

Is there any solution for this problem?

EDIT - full pom.xml

<?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>JavaProjekt</groupId>
<artifactId>JavaProjekt</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180130</version>
    </dependency>
</dependencies>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

Nimander
  • 69
  • 10
  • Please add whole pom.xml to the question. Maven doesn't include dependencies into final jar by default – Ivan Jun 20 '18 at 19:08
  • https://stackoverflow.com/q/16222748/2970947 – Elliott Frisch Jun 20 '18 at 19:08
  • Please provide the whole stacktrace. An exception has to be interpreted according to the context. – davidxxx Jun 20 '18 at 19:09
  • 2
    You have to create an uber jar. Use maven shade plugin – UninformedUser Jun 20 '18 at 19:12
  • 1
    When maven runs your jar it adds the jars you reference to the class path. You could do the same or look into shading. – mrmcgreg Jun 20 '18 at 19:13
  • 1
    U need to create a "fat" jar. Which is a jar that include dependencies. By default, it only build the files you write and 3rd party jars are simply added to the classpath and expect to be there are runtime. See this link: https://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven –  Jun 20 '18 at 19:38
  • 1
    Please don't include answers in the question; add an answer and accept it. Self-answering questions is perfectly okay, and helps prevent confusion. – Dave Newton Jun 20 '18 at 19:41

1 Answers1

1

SOLUTION

pom.xml:

<?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>JavaProjekt</groupId>
<artifactId>JavaProjekt</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>app.App</mainClass>
                            </transformer>
                        </transformers>
                        <shadedArtifactAttached>true</shadedArtifactAttached>
                        <shadedClassifierName>launcher</shadedClassifierName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180130</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>

</dependencies>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

and run by

java -jar target/JavaProjekt-1.0-SNAPSHOT-launcher.jar

Nimander
  • 69
  • 10