Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created May 29, 2013 02:03
Show Gist options
  • Save dcolish/5667489 to your computer and use it in GitHub Desktop.
Save dcolish/5667489 to your computer and use it in GitHub Desktop.
package org.xapian;
import java.net.URL;
public class LoaderUtils {
public static boolean runningInJar() {
String classResourcePath = LoaderUtils.class.getName().replace('.', '/') + ".class";
URL resource = Thread.currentThread()
.getContextClassLoader()
.getResource(classResourcePath);
if (resource != null) {
return resource.getProtocol().contains("jar");
} else {
throw new RuntimeException("Could not find LoaderUtils.class");
}
}
public static void loadDynamic() {
if(runningInJar()) {
// Try to find the library as a resource
String libraryPath = Thread.currentThread().getContextClassLoader().getResource("libxapian_jni.so").getPath();
if (libraryPath != null) {
System.load(libraryPath);
} else {
throw new RuntimeException("Could not find 'libxapian_jni.so");
}
} else {
// Library path has been explicitly set so lets try to use it
try {
System.loadLibrary("xapian_jni");
} catch (UnsatisfiedLinkError error) {
String libraryPath = Thread.currentThread().getContextClassLoader().getResource("libxapian_jni.so").getPath();
System.load(libraryPath);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment