Java by API/java.lang/ClassLoader

Материал из Java эксперт
Перейти к: навигация, поиск

ClassLoader: getSystemClassLoader()

   
public class Main {
  public static void main(String[] args) {
    ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
    new Loaded().go();
  }
}
class Loaded {
  public void go() {
    assert false: "Loaded.go()";
  }
}





ClassLoader: loadClass(String name)

 
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("c:\\");
    URL url = file.toURI().toURL(); 
    URL[] urls = new URL[] { url };
    ClassLoader cl = new URLClassLoader(urls);
    Class cls = cl.loadClass("com.mycompany.MyClass");
  }
}





ClassLoader: setDefaultAssertionStatus(boolean enabled)

   
public class Main {
  public static void main(String[] args) {
    ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
    new Loaded().go();
  }
}
class Loaded {
  public void go() {
    assert false: "Loaded.go()";
  }
}





extends ClassLoader

  
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
class MyClassLoader extends ClassLoader {
  public Class findClass(String name) {
    byte[] classData = null;
    try {
      FileInputStream f = new FileInputStream("C:\\" + name + ".class");
      int num = f.available();
      classData = new byte[num];
      f.read(classData);
    } catch (IOException e) {
      System.out.println(e);
    }
    Class x = defineClass(name, classData, 0, classData.length);
    return x;
  }
}
public class Main {
  static public void main(String args[]) throws Exception {
    MyClassLoader x = new MyClassLoader();
    Class c = x.loadClass(args[0]);
    Class getArg1[] = { (new String[1]).getClass() };
    Method m = c.getMethod("main", getArg1);
    String[] my1 = { "arg1 passed", "arg2 passed" };
    Object myarg1[] = { my1 };
    m.invoke(null, myarg1);
  }
}