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

早过忘川 提交于 2019-11-27 09:21:47

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

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

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