3

I have App.java and pom.xml file below. If I execute App.java run application in Eclipse, it works

However after mvn clean install in eclipse and then open up a terminal and execute the following command to run it,

java -cp myapp-1.0.0-SNAPSHOT.jar org.myapp.test.App

I have error below: I googled it and seems like slf4j-api.jar and jcl-over-slf4j are needed to be included in command line. These jar files are in C:\Users\user.m2\repository\org\slf4j\slf4j-api\1.7.7\slf4j-api-1.7.7.jar and so on.

So I tried the following command line

java -cp C:/Users/user/.m2/repository/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar;C:/Users/user/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.7/jcl-over-slf4j-1.7.7.jar;myapp-1.0.0-SNAPSHOT.jar org.myapp.test.App

This also didn't work.

2) based on my googling, I think I can include such jar files in pom.xml file so that I can run without these error and without adding jar files in command line when I execute it on command line by adding some build path or something like that, but couldn't find clear example.

Could anyone please show some pom.xml file for this?

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at com.walmart.ticketservice.App.<clinit>(App.java:32)
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
Exception in thread "main"   




package org.myapp.test;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    public class App {
        private static final Logger logger = LoggerFactory.getLogger(App.class);

      public static void main( String[] args ) {
          logger.debug("debug");
          logger.info("info");
      }
    }

pom.xml

<dependencies>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.4</version>
  <scope>test</scope>
    </dependency>

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

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.7</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.2</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
                <wtpversion>2.0</wtpversion>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
        </plugin>
    </plugins>
</build>

user2761895
  • 1,431
  • 4
  • 22
  • 38

1 Answers1

3

Eclipse knows what libraries you use and assembles the classpath for you. Doing that manually can be very tedious since you need to consider dependencies, as well.

As you said, you could let Maven bundle everything for you in a jar. Explained here:

http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#jar-with-dependencies

a related question on stackoverflow:

How can I create an executable JAR with dependencies using Maven?

Community
  • 1
  • 1
Jan B.
  • 6,030
  • 5
  • 32
  • 53