Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 1.78 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to Make a Classloader evaluate the classpath before initlializing static objects?
  2. private static void load_libraries() {
  3.     try {
  4.         String osname = getOSName();
  5.  
  6.         if (osname == null) {
  7.             throw new RuntimeException("The system you are running on is not supported");
  8.         }
  9.         URL u = NativeLibs.class.getResource(osname);
  10.  
  11.         URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
  12.         Class urlClass = URLClassLoader.class;
  13.         Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class});
  14.         method.setAccessible(true);
  15.         method.invoke(urlClassLoader, new Object[]{u});
  16.     } catch (IllegalAccessException ex) {
  17.         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
  18.     } catch (IllegalArgumentException ex) {
  19.         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
  20.     } catch (InvocationTargetException ex) {
  21.         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
  22.     } catch (NoSuchMethodException ex) {
  23.         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
  24.     } catch (SecurityException ex) {
  25.         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Failed to load native libs", ex);
  26.     }
  27.  
  28. }
  29.        
  30. private static String getOSName() {
  31.     String os = System.getProperty("os.name").toLowerCase(Locale.US);
  32.     if (os.indexOf("win") >= 0) {
  33.         return "windows";
  34.     } else if (os.indexOf("mac os x") >= 0) {
  35.         return "macosx";
  36.     } else if (os.indexOf("nux") >= 0) {
  37.         return "linux";
  38.     } else if (os.indexOf("solaris") >= 0) {
  39.         return "solaris";
  40.     } else {
  41.         return null;
  42.     }
  43. }