Search code examples
javarenderlwjgldisplay

Java: NoClassDefFoundError in maven project?


I've started developing a 3D java game engine using OpenGL in Codenvy IDE, using Maven. I've configured to run Java 8, and I've set up the dependencies in pom.xml that is required for LWJGL. However, when I run a test window, I get a NoClassDefFoundError. I'm unsure why this happens, considering Maven is configured to store all .Jar dependencies into one filem so accessing them shouldn't be a concern. All the files have downloaded properly into the External Packages directory in my Codenvy project.

Here is 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>engineTester.First3DEngine</groupId>
   <artifactId>First3DEngine</artifactId>
   <version>1.5.7</version>
   <packaging>jar</packaging>

  <dependencies>  
    <dependency>
      <groupId>org.lwjgl.lwjgl</groupId>
      <artifactId>lwjgl</artifactId>
      <version>2.8.4</version>
    </dependency>

    <dependency>
      <groupId>org.lwjgl.lwjgl</groupId>
      <artifactId>lwjgl_util</artifactId>
      <version>2.8.4</version>
    </dependency>

    <dependency>
      <groupId>org.lwjgl.lwjgl</groupId>
      <artifactId>lwjgl_util_applet</artifactId>
      <version>2.8.4</version>
    </dependency>

    <dependency>
      <groupId>com.typesafe.slick</groupId>
      <artifactId>slick_2.10</artifactId>
      <version>3.1.1</version>
    </dependency>
    <dependency>
      <groupId>java3d</groupId>
      <artifactId>vecmath</artifactId>
  <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <!-- Build an executable JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <argLine>-Xmx1024m</argLine>
          <archive>
            <manifest>
          <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>engineTester.MainGameLoop</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
   </build>
</project>

And here is where I'm getting the issue:

package engineTester;

import org.lwjgl.opengl.Display;

import renderEngine.DisplayManager;
import util.Console;

public class MainGameLoop {
    public static void main(String[] args){
        new Console();
        DisplayManager.createDisplay();  <-- line which throws the error
        while(!Display.isCloseRequested()){
            //game logic
            //render
            DisplayManager.updateDisplay();
        }

        DisplayManager.closeDisplay();

    }
}

DisplayManager.createDisplay() method:

public static void createDisplay(){
    ContextAttribs attribs = new ContextAttribs(3,2);
    attribs.withForwardCompatible(true);
    attribs.withProfileCore(true);

    try{
        Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
        Display.create(new PixelFormat(), attribs);
    } catch (LWJGLException e){
        e.printStackTrace();
    }

    GL11.glViewport(0,0,WIDTH,HEIGHT);
}

And finally the full error:

Exception in thread "main" java.lang.NoClassDefFoundError:     org/lwjgl/LWJGLException
    at engineTester.MainGameLoop.main(MainGameLoop.java:11)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
    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

Thanks for your help!


Solution

  • You can either use maven-assembly-plugin to make a fat jar that can be run by itself or you can run your program using maven so that the compile time jars will be added to your classpath.

    To run using maven:

    mvn exec:java -Dexec.mainClass="engineTester.MainGameLoop"
    

    To use the assembly plugin:

    <build>
        <plugins>
             <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>engineTester.MainGameLoop</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </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>
    

    This will create a jar with -jar-with-depencencies added to the name in the target directory. That can then just be run with java -jar jarfile.