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

前端 未结 2 1963
遇见更好的自我
遇见更好的自我 2020-12-01 23:27
Exception in thread \"main\" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at j         


        
相关标签:
2条回答
  • 2020-12-01 23:42

    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

    0 讨论(0)
  • 2020-12-01 23:52

    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());
    
    0 讨论(0)
提交回复
热议问题