Reverse the JNI load order to look for system library first. (#145)

This commit is contained in:
Peter Johnson
2016-10-25 22:27:33 -07:00
committed by GitHub
parent 273a395a2f
commit 778edaeb28

View File

@@ -15,53 +15,53 @@ public class NetworkTablesJNI {
static {
if (!libraryLoaded) {
try {
String osname = System.getProperty("os.name");
String resname;
if (osname.startsWith("Windows"))
resname = "/Windows/" + System.getProperty("os.arch") + "/";
else
resname = "/" + osname + "/" + System.getProperty("os.arch") + "/";
System.out.println("platform: " + resname);
if (osname.startsWith("Windows"))
resname += "ntcore.dll";
else if (osname.startsWith("Mac"))
resname += "libntcore.dylib";
else
resname += "libntcore.so";
InputStream is = NetworkTablesJNI.class.getResourceAsStream(resname);
if (is != null) {
// create temporary file
if (System.getProperty("os.name").startsWith("Windows"))
jniLibrary = File.createTempFile("NetworkTablesJNI", ".dll");
else if (System.getProperty("os.name").startsWith("Mac"))
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".dylib");
System.loadLibrary("ntcore");
} catch (UnsatisfiedLinkError e) {
try {
String osname = System.getProperty("os.name");
String resname;
if (osname.startsWith("Windows"))
resname = "/Windows/" + System.getProperty("os.arch") + "/";
else
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".so");
// flag for delete on exit
jniLibrary.deleteOnExit();
OutputStream os = new FileOutputStream(jniLibrary);
resname = "/" + osname + "/" + System.getProperty("os.arch") + "/";
System.out.println("platform: " + resname);
if (osname.startsWith("Windows"))
resname += "ntcore.dll";
else if (osname.startsWith("Mac"))
resname += "libntcore.dylib";
else
resname += "libntcore.so";
InputStream is = NetworkTablesJNI.class.getResourceAsStream(resname);
if (is != null) {
// create temporary file
if (System.getProperty("os.name").startsWith("Windows"))
jniLibrary = File.createTempFile("NetworkTablesJNI", ".dll");
else if (System.getProperty("os.name").startsWith("Mac"))
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".dylib");
else
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".so");
// flag for delete on exit
jniLibrary.deleteOnExit();
OutputStream os = new FileOutputStream(jniLibrary);
byte[] buffer = new byte[1024];
int readBytes;
try {
while ((readBytes = is.read(buffer)) != -1) {
os.write(buffer, 0, readBytes);
byte[] buffer = new byte[1024];
int readBytes;
try {
while ((readBytes = is.read(buffer)) != -1) {
os.write(buffer, 0, readBytes);
}
} finally {
os.close();
is.close();
}
} finally {
os.close();
is.close();
}
try {
System.load(jniLibrary.getAbsolutePath());
} catch (UnsatisfiedLinkError e) {
} else {
System.loadLibrary("ntcore");
}
} else {
System.loadLibrary("ntcore");
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1);
}
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1);
}
libraryLoaded = true;
}