JarClassLoader.javaAPI DocExample2346Tue Dec 12 18:58:00 GMT 2000None


public class JarClassLoader extends URLClassLoader
A class loader for loading jar files, both local and remote.

Fields Summary
private URL
Constructors Summary
public JarClassLoader(URL url)
Creates a new JarClassLoader for the specified url.

url the url of the jar file

	super(new URL[] { url });
	this.url = url;
Methods Summary
public java.lang.StringgetMainClassName()
Returns the name of the jar file main class, or null if no "Main-Class" manifest attributes was defined.

	URL u = new URL("jar", "", url + "!/");
	JarURLConnection uc = (JarURLConnection)u.openConnection();
	Attributes attr = uc.getMainAttributes();
	return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
public voidinvokeClass(java.lang.String name, java.lang.String[] args)
Invokes the application in this jar file given the name of the main class and an array of arguments. The class must define a static method "main" which takes an array of String arguemtns and is of return type "void".

name the name of the main class
args the arguments for the application
ClassNotFoundException if the specified class could not be found
NoSuchMethodException if the specified class does not contain a "main" method
InvocationTargetException if the application raised an exception

	Class c = loadClass(name);
	Method m = c.getMethod("main", new Class[] { args.getClass() });
	int mods = m.getModifiers();
	if (m.getReturnType() != void.class || !Modifier.isStatic(mods) ||
	    !Modifier.isPublic(mods)) {
	    throw new NoSuchMethodException("main");
	try {
	    m.invoke(null, new Object[] { args });
	} catch (IllegalAccessException e) {
	    // This should not happen, as we have disabled access checks