Last active
July 9, 2017 15:47
-
-
Save malobre/38e60574c7807b20e441 to your computer and use it in GitHub Desktop.
Simple class that extract and load the lwjgl 3 natives from your jar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This code is under the CC BY 4.0 license, http://creativecommons.org/licenses/by/4.0/ | |
*/ | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.channels.Channels; | |
import java.nio.channels.ReadableByteChannel; | |
/** | |
* Load lwjgl natives from a folder inside your jar using modern java (1.8). | |
* | |
* @author Maël "Malobre" Obréjan | |
*/ | |
public class NativesLoader { | |
private static boolean isWindows = System.getProperty("os.name").contains("Windows"); | |
private static boolean isLinux = System.getProperty("os.name").contains("Linux"); | |
private static boolean isMac = System.getProperty("os.name").contains("Mac"); | |
private static boolean is64Bit = System.getProperty("os.arch").equals("amd64") || System.getProperty("os.arch").equals("x86_64"); | |
/** | |
* Load lwjgl natives from the jar root folder | |
* @throws Exception if we are unable to load the natives | |
*/ | |
public static void load() throws Exception { | |
load("/"); | |
} | |
/** | |
* Load lwjgl natives from the specified jar folder | |
* @param root the jar folder where the natives are located | |
* @throws Exception if we are unable to load the natives | |
*/ | |
public static void load(String root) throws Exception { | |
File nativesDirectory = new File(System.getProperty("java.io.tmpdir"), "LWJGL-natives"); | |
nativesDirectory.mkdirs(); | |
if (isWindows) { | |
extract(is64Bit ? "lwjgl.dll" : "lwjgl32.dll" , root, nativesDirectory); | |
extract(is64Bit ? "OpenAL.dll" : "OpenAL32.dll", root, nativesDirectory); | |
} else if (isLinux) { | |
extract(is64Bit ? "liblwjgl.so" : "liblwjgl32.so" , root, nativesDirectory); | |
extract(is64Bit ? "libopenal.so" : "libopenal32.so", root, nativesDirectory); | |
} else if (isMac) { | |
extract("liblwjgl.dylib" , root, nativesDirectory); | |
extract("libopenal.dylib", root, nativesDirectory); | |
} else { | |
throw new Exception("Unable to detect the operating system, aborting lwjgl natives loading."); | |
} | |
System.setProperty("org.lwjgl.librarypath", nativesDirectory.getAbsolutePath()); | |
} | |
private static void extract(String file, String from, File to) throws Exception { | |
try (InputStream inputStream = NativesLoader.class.getResourceAsStream(from + "/" + file)) { | |
if (inputStream == null) | |
throw new Exception("Cannot extract native '" + file + "'"); | |
ReadableByteChannel rbc = Channels.newChannel(inputStream); | |
File outputFile = new File(to, file); | |
outputFile.createNewFile(); | |
try (FileOutputStream fos = new FileOutputStream(outputFile)) { | |
fos.getChannel().transferFrom(rbc, 0, 1 << 20); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment