0

hi i know this question is been asked before but im not able to find a solution for this. im getting this when i run my jar

$ java -jar target/hi-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at test.hi.App.main(App.java:20)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

this is my pom depedencies

 <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.30</version>
   </dependency>
 
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.7.30</version>
   </dependency>

this is my main class

import org.slf4j.*;

public class App {
    
    public static void main( String[] args )  {
        Logger log = LoggerFactory.getLogger(App.class);
        Properties prop = new Properties();
        Scanner scan = new Scanner(System.in);

        log.info("ENTER YOUR ID:");
        String SOEID = scan.nextLine();
        log.info("ENTER YOUR PASSWORD :");
        String PASSWORD = scan.nextLine();
        scan.close();
}

any help...im using maven repository outside of project to store all the libraries..

pom.xml

<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>test</groupId>
  <artifactId>hi</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hi</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 <!--<build>
  <plugins>
    <plugin>

      <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>test.hi.App</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>-->
<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>test.hi.App</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>
<repositories>
    <repository>
      <id>jgit-repository</id>
      <url>https://repo.eclipse.org/content/groups/releases/</url>
    </repository>
  </repositories>
  
  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
      <scope>test</scope>
    </dependency>
     

  <!-- Core Library -->

    <dependency>
      <groupId>org.eclipse.jgit</groupId>
      <artifactId>org.eclipse.jgit</artifactId>
      <version>5.10.0.202012080955-r</version>
    </dependency>
  

  <!-- Smart HTTP Servlet -->
  
   <dependency>
      <groupId>org.eclipse.jgit</groupId>
      <artifactId>org.eclipse.jgit.http.server</artifactId>
      <version>5.10.0.202012080955-r</version>
    </dependency>
 

  <!-- AWT UI Helpers -->
  
    <dependency>
      <groupId>org.eclipse.jgit</groupId>
      <artifactId>org.eclipse.jgit.ui</artifactId>
      <version>5.10.0.202012080955-r</version>
    </dependency>
 
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.30</version>
   </dependency>
 
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.7.30</version>
   </dependency>

   <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.8.0</version>
</dependency>

<dependency>
    <groupId>org.eclipse.jgit</groupId>
    <artifactId>org.eclipse.jgit.ssh.jsch</artifactId>
    <version>5.10.0.202012080955-r</version>
</dependency>

    </dependencies>
</project>
  • How are you creating your jar file? Probably at the generation, you are not including the dependencies in the final package – Tavo Sanchez Apr 28 '21 at 14:31
  • @TavoSanchez i create maven install on the pom file ..which creates a jar in the taget folder [INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ hi --- [INFO] Building jar: C:\Users\james\eclipse-workspace\hi\target\hi-0.0.1-SNAPSHOT.jar [INFO] like this – Davertonator Apr 28 '21 at 14:35
  • @TavoSanchez do u know how to include dependencies in pom file build configurations when doing maven build? – Davertonator Apr 28 '21 at 14:41
  • Ok, as it is right now (I'm guessing) you don't have anything to copy the dependencies. So, you need to use a plugin to copy the dependencies into your jar, you can use [Maven Assembly Plugin](http://maven.apache.org/plugins/maven-assembly-plugin/) or [Maven Dependency Plugin](https://maven.apache.org/plugins/maven-dependency-plugin/). You might want to check this https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – Tavo Sanchez Apr 28 '21 at 14:42
  • @TavoSanchez i put in my whole pom.xml above in question – Davertonator Apr 28 '21 at 14:50
  • 1
    With that configuration, you can run `mvn clean compile assembly:single`. Please check the answer in the link I sent to complete the configuration and make it work directly with `mvn install` – Tavo Sanchez Apr 28 '21 at 14:59
  • @TavoSanchez im see this now $ java -jar target/hi-0.0.1-SNAPSHOT-jar-with-dependencies.jar log4j:WARN No appenders could be found for logger (test.hi.App). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. it is not printing out the test message i have in cmd – Davertonator Apr 28 '21 at 15:09
  • @TavoSanchez this doesnt add my config.properties and log4j.properties file in the jar when i opened the jar – Davertonator Apr 28 '21 at 15:27
  • That's out of scope of your original question. However, double-check that your properties file is in the resources directory `src/main/resources/log4j.properties` and check the descriptors section of the Maven Assembly plugin where you can create an Assembly file with all the files to include/exclude (there you should put your properties file) and add it to the plugin configuration – Tavo Sanchez Apr 28 '21 at 16:51
  • i didnt do anything apart from moving to resource folder and it worked @TavoSanchez – Davertonator Apr 28 '21 at 18:29

0 Answers0