Search code examples
javalinuxwindowsmacoslwjgl

LWJGL Automatic Native Picker


In LWJGL, (I am on a mac), I am making a program in it but as I debug/run my code, it requires the LWJGL OS X natives to run.

Same goes for Windows, Linux or anything else but I want it to be a little bit like the game Minecraft which was made in LWJGL.

You start the game and it automatically picks your operating systems natives.

Is there a way I can do this so I dont have to switch around manually on different operating systems?

Please share your source if you know!!


Solution

  • Have a look at this: http://wiki.lwjgl.org/wiki/Distributing_Your_LWJGL_Application.html.

    It says that you can set the path to your natives by calling the following at the beginning of your main method:

    System.setProperty("org.lwjgl.librarypath", path);
    

    By doing some OS detection you should be able to load the right natives, like so:

    if (System.getProperty("os.name").contains("Windows")) {
        // Windows
        System.setProperty("org.lwjgl.librarypath", new File("lwjglFolderLocation/native/windows").getAbsolutePath());
    } else if (System.getProperty("os.name").contains("Mac")) {
        // Mac OS X
        System.setProperty("org.lwjgl.librarypath", new File("lwjglFolderLocation/native/macosx").getAbsolutePath());
    } else if (System.getProperty("os.name").contains("Linux")) {
        // Linux
        System.setProperty("org.lwjgl.librarypath", new File("lwjglFolderLocation/native/linux").getAbsolutePath());
    } else if (System.getProperty("os.name").contains("Sun")) {
        // SunOS (Solaris)
        System.setProperty("org.lwjgl.librarypath", new File("lwjglFolderLocation/native/solaris").getAbsolutePath());
    } else {
        throw new RuntimeException("Your OS is not supported");
    }