
Untitled
By: a guest on
Jun 25th, 2012 | syntax:
None | size: 1.78 KB | hits: 15 | expires: Never
How to Make a Classloader evaluate the classpath before initlializing static objects?
private static void load_libraries() {
try {
String osname = getOSName();
if (osname == null) {
throw new RuntimeException("The system you are running on is not supported");
}
URL u = NativeLibs.class.getResource(osname);
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[]{u});
} catch (IllegalAccessException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
} catch (SecurityException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
}
}
private static String getOSName() {
String os = System.getProperty("os.name").toLowerCase(Locale.US);
if (os.indexOf("win") >= 0) {
return "windows";
} else if (os.indexOf("mac os x") >= 0) {
return "macosx";
} else if (os.indexOf("nux") >= 0) {
return "linux";
} else if (os.indexOf("solaris") >= 0) {
return "solaris";
} else {
return null;
}
}