tsaebeht
tsaebeht

Reputation: 1680

Maven build failing after adding classpath and dependencies in lib folder in pom.xml NoClassDefFoundError

I have an app I built in Java and successfully compiled in Maven. After building I go to the target folder and run java -jar *projectfilename*.jar and I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jibble/pircbot/PircBot
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    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)
    at io.github.bholagabbar.Main.setupIRCBot(Main.java:9)
    at io.github.bholagabbar.Main.main(Main.java:27) Caused by: java.lang.ClassNotFoundException: org.jibble.pircbot.PircBot
    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)

Now org/jibble/pircbot/PircBot which was not found is actually one of my dependencies in my 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>io.github.pixrat</groupId>
  <artifactId>gallurdo</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <name>gallurdo</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-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>
                ${project.build.directory}/lib
              </outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>io.github.pixrat.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>pircbot</groupId>
      <artifactId>pircbot</artifactId>
      <version>1.5.0</version>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.github.Ullink</groupId>
      <artifactId>simple-slack-api</artifactId>
      <version>0.5.1</version>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

I have referred to answer here Maven: NoClassDefFoundError in the main thread, Maven: NoClassDefFoundError in the main thread and many other places but in vain. I'd appreciate some help thanks

Upvotes: 0

Views: 1539

Answers (2)

ujulu
ujulu

Reputation: 3309

Here is the JavaDoc for the NoClassDefFoundException:

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

and the scope provided means that the JDK or the container provide the dependency. As you are trying to execute your program as standalone on Java SE and if your library is not part of JDK your dependency will not be available at runtime, and hence throwing this exception.

So either remove the <scope>provided</scope> so that the default applies or change it to compile instead.

Upvotes: 3

RITZ XAVI
RITZ XAVI

Reputation: 3789

You will have to use the maven shade plugin. For Eg.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <createDendencyReducedPom>false</createDependencyReducedPom>      // this is optional
            </configuration>
        </execution>
    </executions>
</plugin>

What this will do is, it will create one single jar which would contain your compiled java code as well as the contents of all the jar dependencies that you mention in you pom. This is almost like you extracting all the jars in some location and creating one single jar out of this location.

Upvotes: 0

Related Questions