Created
May 29, 2013 02:03
-
-
Save dcolish/5667489 to your computer and use it in GitHub Desktop.
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
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