Exemplo n.º 1
0
 public boolean isClassLoaded(String className) {
   try {
     ClassLoader cl = getClassLoader();
     while (cl != null) {
       // get loaded classes
       Vector<?> loadedClasses = Util.getField(cl, ClassLoader.class, "classes", Vector.class);
       for (Object object : loadedClasses) {
         Class<?> clz = (Class<?>) object;
         if (clz.getName().equals(className)) {
           return true;
         }
       }
       cl = cl.getParent();
     }
     // check bootstrap classes
     Resource resource = Launcher.getBootstrapClassPath().getResource(Util.resolveName(className));
     return resource != null;
   } catch (RuntimeException e) {
     if (reportErrorNoPermission) {
       throw e;
     } else {
       return false;
     }
   }
 }
Exemplo n.º 2
0
 public void testBootClassload() {
   // 获取根类加载器所加载的全部URL数组
   URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
   for (int i = 0; i < urls.length; i++) {
     System.out.println(urls[i].toExternalForm());
   }
   System.out.println(System.getProperty("sun.boot.class.path"));
 }
  public static void main(String[] args) {
    URL[] urls = Launcher.getBootstrapClassPath().getURLs();
    for (int i = 0; i < urls.length; i++) {
      System.out.println(urls[i].toExternalForm());
    }

    System.out.println(System.getProperty("sun.boot.class.path"));
  }
Exemplo n.º 4
0
  public static void main(String[] args) {
    // 获取根类加载器所加载的全部URL数组
    URL[] urls = sun.misc.Launcher.getBootstrapClassPath().getURLs();
    // 遍历、输出根类加载器加载的全部URL
    for (int i = 0; i < urls.length; i++) {
      System.out.println(urls[i].toExternalForm());
    }

    for (URL url : urls) System.out.println(url);
  }
Exemplo n.º 5
0
 public ClassLoader[] findClassLoaders(String className) {
   List<ClassLoader> list = new ArrayList<ClassLoader>();
   try {
     ClassLoader cl = getClassLoader();
     while (cl != null) {
       Vector<?> loadedClasses = Util.getField(cl, ClassLoader.class, "classes", Vector.class);
       boolean founded = false;
       for (Object object : loadedClasses) {
         Class<?> clz = (Class<?>) object;
         if (clz.getName().equals(className)) {
           list.add(clz.getClassLoader());
           founded = true;
           break;
         }
       }
       if (!founded) {
         Class<?> clz;
         try {
           clz =
               Util.invokeMethod(
                   cl,
                   ClassLoader.class,
                   "findClass",
                   new Class[] {String.class},
                   new Object[] {className},
                   Class.class);
           list.add(clz.getClassLoader());
         } catch (InvocationTargetException e) {
           if (!(e.getCause() instanceof ClassNotFoundException)) {
             // ignore class not found
             if (e.getCause() instanceof SecurityException) {
               if (reportErrorNoPermission) {
                 throw (SecurityException) e.getCause();
               }
             }
           }
         }
       }
       cl = cl.getParent();
     }
     // check bootstrap classes
     Resource resource = Launcher.getBootstrapClassPath().getResource(Util.resolveName(className));
     if (resource != null) {
       // bootstrap classloader in first
       list.add(0, null);
     }
   } catch (RuntimeException e) {
     if (reportErrorNoPermission) {
       throw e;
     }
   }
   return list.toArray(new ClassLoader[list.size()]);
 }