1

I am completely new to maven, took a couple of tutorials and decided to do some logging with slf4j but I keep getting an error that the class cannot be found.

After some internet searches I have tried to add plugins etc. to my pom.xml but nothing seem to work.

The exception is this:

java -cp target/MavenTest-1.0-SNAPSHOT.jar org.nilun.App
Hello World!
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.nilun.App.main(App.java:15)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

Here is a snippet of my pom.xml

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>       
    </dependencies>

The very simple class

package org.nilun;    
import org.slf4j.*;
/**
 * Hello world!
 *
 */
public class App 
{   
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        Logger logger = LoggerFactory.getLogger(App.class);
        logger.info("Hello World!");
    }    
}

The jar file is located in my $HOME/development/lib And of course it has been added to my eclipse build path

Any ideas would be welcomed to solve this.

Thanks!

2 Answers2

1

I think the way to execute the program is the problem. If I execute your code in Eclipse, it works just fine (after adding this dependency

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.12</version>
</dependency>

which you will need to fix another exception, according to SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"). However, when I use "java -cp target/MavenTest-1.0-SNAPSHOT.jar org.nilun.App", I get the same exception as you.

Edit: To create a .jar file containing dependencies, take a look at the Maven Assembly Plugin (here is a nice and short introduction: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html). You need to add the following to your POM:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>de.clanue.MavenTest.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Now, building the project with maven creates a file "MavenTest-0.0.1-SNAPSHOT-jar-with-dependencies.jar" in the target folder. It contains your dependencies and you can run it using "java -jar target/MavenTest-0.0.1-SNAPSHOT-jar-with-dependencies.jar".

Community
  • 1
  • 1
Lupa
  • 78
  • 8
0

Hi Lupa and thank you for the help. With your guidance I got it to work. What I did was to add the following plugins to

  1. copy the folders to the target/lib folder
  2. Add the /lib....jar to the manifest file.

Thanks again for the help!

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>false</overWriteSnapshots>
              <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>