user1914539
user1914539

Reputation:

LWJGL NoClassDefFoundError

I am currently running Linux Mint 14 Nadia 64bit with Java 7 update 21, and I'm using LWGL 2.9. I am also using a makefile to compile and run everything.

My problem I have is when I try to run my JAR (make run) that I compiled from command line (see source below), I get this error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
    at java.lang.Class.getMethod0(Class.java:2694)
    at java.lang.Class.getMethod(Class.java:1622)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 6 more
make: *** [run] Error 1

Before, I had the same problem with compiling via javac (via make), but I fixed it by just adding the -cp $(LIB_FILES) flag to the javac command. So, I tried adding that to the java command (via make run), but it didn't fix my problem.

My next guess was that I needed to set Djava.library.path to the native folder, which also did not work. I have supplied all the information I think is needed, but if you need more, just ask.

Directory Layout:

Platform-Jumper
    + class
        + net/netne/platinumcoding/platformer    (shortened to save space)
            - Main.class
    + dist
        - Executable.jar
    + lib
        + native
            + (freeusb/linux/macosx/solaris/windows)
        - jinput.jar
        - lwjgl.jar
        - lwjgl_util.jar
    - Makefile
    - MANIFEST.MF
    - README.md
    + res
        (Image files)
    + src
        + net/netne/platinumcoding/platformer    (shortened to save space)
            - Main.java

(Credit to Manzill0 for the original Makefile) Makefile:

JC              := javac
JAR             := jar

MODULES         := net/netne/platinumcoding/platformer
SRC_DIR         := $(addprefix src/,$(MODULES))
CLASS_DIR       := $(addprefix class/,$(MODULES))
SO_DIR          := "lib/native/linux"
LIB_FILES       := lib/lwjgl.jar:lib/lwjgl_util.jar:lib/jinput.jar

SRC             := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.java))
OBJ             := $(patsubst src/%.java,class/%.class,$(SRC))

vpath %.java $(SRC_DIR)

.PHONY: all checkdirs clean

all: checkdirs dist/Executable.jar

dist/Executable.jar: $(OBJ)
        $(JAR) cvfm $@ MANIFEST.MF -C $(CLASS_DIR)/ .

$(OBJ): $(SRC)
        $(JC) -cp $(LIB_FILES) -d $(CLASS_DIR) $<

checkdirs: $(SRC_DIR) $(CLASS_DIR) $(LIB_DIR)

$(CLASS_DIR):
        @mkdir -p $@

clean:
        @rm -rf $(BUILD_DIR)
        @rm -f dist/Executable.jar

run:
        java -cp $(LIB_FILES) -Djava.library.path=$(SO_DIR) -jar dist/Executable.jar

Main.java:

package net.netne.platinumcoding.platformer;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Main {
        public static void main(String[] args) {
                new Main().start();
        }

        public void start() {
                try {
                        Display.setDisplayMode(new DisplayMode(800, 600));
                } catch (LWJGLException e) {
                        e.printStackTrace();
                }

                while (!Display.isCloseRequested()) {
                        Display.update();
                        Display.sync(60);
                }

                Display.destroy();
        }
}

Note: If you need the content of files, I have this uploaded to GitHub @ https://github.com/DealerNextDoor/Platform-Jumper

Upvotes: 1

Views: 4516

Answers (1)

user1914539
user1914539

Reputation:

I found out what my problem was. I was mixing the -jar flag with the -cp flag. To fix this problem, all I had to do was add the JAR file to my classpath, and then run my main class.

So instead of having:

-cp lib/lwjgl.jar:lib/lwjgl_util.jar:lib/jinput.jar

I would use

-cp lib/lwjgl.jar:lib/lwjgl_util.jar:lib/jinput.jar:dist/Executable.jar

And this fixes the problem.

Upvotes: 1

Related Questions