0

I have an app that runs well if I start it using the Intellij button after a mvn clean install (all using the GUI).

I try to run the generated jar file using the command line:

java -jar myJar.jar

But I can't stop running into these errors:

java -jar .\myJar-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at data.SecretProvider.<clinit>(SecretProvider.java:14)

Again, if I run the app using Intellij, no errors and the logs work.

I think it's the way I package my jar in the pom.xml, but I have very to no knowledge about it. Here's the 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>org.bouji</groupId>
<artifactId>myjar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<repositories>
    <repository>
        <id>dv8tion</id>
        <name>m2-dv8tion</name>
        <url>https://m2.dv8tion.net/releases</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>net.dv8tion</groupId>
        <artifactId>JDA</artifactId>
        <version>5.0.0-alpha.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>com.sedmelluq</groupId>
        <artifactId>lavaplayer</artifactId>
        <version>1.3.77</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.163</version>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jool</artifactId>
        <version>0.9.14</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.21.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.960</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>CatBot</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Tried reading similar issues on here, but it seems to always be specific to the error/ide.

Bouji
  • 184
  • 2
  • 11

1 Answers1

0

In jar's MANIFEST file there must be dependencies that needed for the application to run. Ensure that you have them.

Also -jar doesn't include classpath other than file itself. You can try to run with:

java -cp <path>myJar-SNAPSHOT.jar;<path to missing> <your main class>
Baran Bursalı
  • 360
  • 1
  • 7