LWJGL 'java.lang.UnsatisfiedLinkError': no lwjgl in java.library.path

让人想犯罪 __ 提交于 2019-11-26 14:39:37

问题


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!


回答1:


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());



回答2:


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



来源:https://stackoverflow.com/questions/24592513/lwjgl-java-lang-unsatisfiedlinkerror-no-lwjgl-in-java-library-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!