Example usage for java.net URLClassLoader loadClass

List of usage examples for java.net URLClassLoader loadClass

Introduction

In this page you can find the example usage for java.net URLClassLoader loadClass.

Prototype

public final Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException 

Source Link

Usage

From source file:MainClass.java

public static void main(String args[]) {
    String name = "http://urlWithClassName";
    try {/*  ww  w . j a  va 2  s.  c  om*/
        if (!name.endsWith(".class")) {
            System.err.println("That doesn't look like a byte code file!");
            return;
        }
        URL u = new URL(name);
        URLClassLoader ucl = new URLClassLoader(u);

        // parse out the name of the class from the URL
        String s = u.getFile();
        String classname = s.substring(s.lastIndexOf('/'), s.lastIndexOf(".class"));
        Class AppletClass = ucl.loadClass(classname, true);
        Applet apl = (Applet) AppletClass.newInstance();
        JFrame f = new JFrame();
        f.setSize(200, 200);
        f.add("Center", apl);
        apl.init();
        apl.start();
        f.setVisible(true);
    } catch (Exception e) {
        System.err.println(e);
    }
}