5
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at org.lwjgl.Sys$1.run(Sys.java:73)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
        at org.lwjgl.Sys.loadLibrary(Sys.java:95)
        at org.lwjgl.Sys.<clinit>(Sys.java:112)
        at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
        at org.lorana.client.Lorana.<init>(Lorana.java:20)
        at org.lorana.client.Lorana.main(Lorana.java:31)

The error still persists after I've linked all native libraries to every referenced library, and followed the instructions of http://ninjacave.com/lwjglwitheclipse

I've also followed other questions on the board regarding lwjgl unsatisfiedlinkerrors, but to no avail.

Would very much appreciate the help, Thanks in advance!

firt
  • 65
  • 1
  • 5
  • This error means that the JVM cannot find the native library `lwjgl.dll`, so something must be configured wrong. Without more information on exactly how your project is configured, it's very hard to give you a more useful answer. – Jesper Jul 06 '14 at 07:43

2 Answers2

5

LWJGL uses its own variables for the path to the native libraries:

 System.setProperty("org.lwjgl.librarypath", new File("pathToNatives").getAbsolutePath());


If you kept the file structure from the LWJGL package you can use something like this:

    switch(LWJGLUtil.getPlatform())
    {
        case LWJGLUtil.PLATFORM_WINDOWS:
        {
            JGLLib = new File("./native/windows/");
        }
        break;

        case LWJGLUtil.PLATFORM_LINUX:
        {
            JGLLib = new File("./native/linux/");
        }
        break;

        case LWJGLUtil.PLATFORM_MACOSX:
        {
            JGLLib = new File("./native/macosx/");
        }
        break;
    }

    System.setProperty("org.lwjgl.librarypath", JGLLib.getAbsolutePath());
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
  • would I need to have the natives in a sub folder with the main runnable jar? – firt Jul 07 '14 at 22:15
  • The property has to be set to the full path to the native libs. It doesn't matter where they are as long as the path fits. The second snippet is build for a structure where the libs are in a path directly next to the main jar and they still use the subfolders from the LWJGL package. – Dawnkeeper Jul 08 '14 at 05:22
  • Thanks so much! I managed to get it to work by using the setProperty method to direct it to /natives/windows, which looks for the file within the same directory as the exported jar. – firt Jul 09 '14 at 00:41
0

Not sure about getting it to work in Eclipse BUT I encountered similar problems in my attempt to build an executable JAR.

All of the solutions below assume you have the native libraries either alongside the JAR file in the same directory, or bundled into the JAR as embedded resources.

As @Dawnkeeper describes, you can simply use the "org.lwjgl.librarypath" system property to instruct LWJGL where to find the native libraries.

OR

As your error suggests, LWJGL checks the more-common "java.library.path" system property to locate the native libraries. You can set this at the command line when you run your JAR like so:

java -Djava.library.path=./lib -jar myApplication.jar

As I mentioned above though, I wanted a stand-alone executable JAR; I didn't want the user to have to run the JAR file with command-line arguments. I attempted to set this system property from within my main method but soon discovered that you cannot change this system property's value after the JVM runtime has been initialized. Instead, I ended up writing the following code (using the work-around linked above) to set the "java.library.path" within my main method:

public static void main(String[] args) {
    addLwjglNativesToJavaLibraryPathProperty();
    // run code dependent on LWJGL here...
}

private static void addLwjglNativesToJavaLibraryPathProperty() {
    String osDir;
    if (SystemUtils.IS_OS_WINDOWS) {
        osDir = "windows";
    } else if (SystemUtils.IS_OS_LINUX) {
        osDir = "linux";
    } else if (SystemUtils.IS_OS_MAC_OSX) {
        osDir = "macosx";
    } else if (SystemUtils.IS_OS_SOLARIS) {
        osDir = "solaris";
    } else {
        throw new RuntimeException("Unsupported OS: " + System.getProperty("os.name"));
    }
    addPathToJavaLibraryPathProperty("lib/natives/" + osDir);
}

// https://stackoverflow.com/q/5419039
private static void addPathToJavaLibraryPathProperty(String propertyValue) {
    String propertyName = "java.library.path";
    try {
        Field field = ClassLoader.class.getDeclaredField("usr_paths");
        field.setAccessible(true);
        String[] paths = (String[]) field.get(null);
        for (String path : paths) {
            if (propertyValue.equals(path)) return;
        }
        String[] tmp = new String[paths.length + 1];
        System.arraycopy(paths, 0, tmp, 0, paths.length);
        tmp[paths.length] = propertyValue;
        field.set(null, tmp);
        System.setProperty(propertyName, System.getProperty(propertyName) + File.pathSeparator + propertyValue);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("Failed to get permissions to set " + propertyName);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException("Failed to get field handle to set " + propertyName);
    }
}

Code Reference: https://code.google.com/p/gwahtzee/source/browse/trunk/src/main/java/com/googlecode/gwahtzee/Application.java

Community
  • 1
  • 1
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143