问题
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!!
回答1:
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");
}
回答2:
As of LWJGL 3 there is a SharedLibraryLoader that handles loading the correct natives for you. It extracts natives from jars in the classpath, so you can for example bundle all natives in a single jar or have a separate jar for each native.
The jars have to be on the classpath, so when you distribute your program as an executable jar you have to set the Class-Path
manifest attribute correctly.
The loader should be active by default, but you can explicitly activate it with
Library.initialize();
It works by extracting the natives from the jars into a temporary directory and loading them from there. If you want, you can change the temporary directory name and path through the SHARED_LIBRARY_EXTRACT_DIRECTORY
and SHARED_LIBRARY_EXTRACT_PATH
Configuration properties.
来源:https://stackoverflow.com/questions/24850014/lwjgl-automatic-native-picker