package com.sun.java;

import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * Runs a jar application from any url. Usage is 'java JarRunner url [args..]'
 * where url is the url of the jar file and args is optional arguments to
 * be passed to the application's main method.
 */
public class JarRunner {
    public static void main(String[] args) {
	if (args.length < 1) {
		usage();
		}
	for(StringTokenizer tokenizer = new StringTokenizer(args[0]); tokenizer.hasMoreTokens();) {
		URL url = null;
		String next = tokenizer.nextToken();
		try {
			url = new URL(next);
			}
		catch (MalformedURLException e) {
			fatal("Invalid URL: " + next);
			continue;
			}
		// Create the class loader for the application jar file
		JarClassLoader cl = new JarClassLoader(url);
		// Get the application's main class name
		String name = null;
		try {
			name = cl.getMainClassName();
			}
		catch (IOException e) {
			System.err.println("I/O error while loading JAR file: "+e);
			continue;
			}
		if (name == null) {
			fatal("Specified jar file does not contain a 'Main-Class'" +
				" manifest attribute");
			continue;
			}
		// Get arguments for the application
		String[] newArgs = new String[args.length - 1 ];
		System.arraycopy(args, 1, newArgs, 0, newArgs.length);
		// Invoke application's main class
		try {
		    cl.invokeClass(name, newArgs);
		} catch (ClassNotFoundException e) {
		    fatal("Class not found: " + name);
		    continue;
		} catch (NoSuchMethodException e) {
		    fatal("Class does not define a 'main' method: " + name);
		    continue;
		} catch (InvocationTargetException e) {
	    	    e.getTargetException().printStackTrace();
		    continue;
		}
	}
    }

    private static void fatal(String s) {
	System.err.println(s);
	//System.exit(1);
    }

    private static void usage() {
	fatal("Usage: java JarRunner [urls] [args..]");
    }
}
