服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

从任意位置运行jar文件

/* from http://java.sun.com/docs/books/tutorial/index.html */


import java.io.ioexception;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.lang.reflect.modifier;
import java.net.jarurlconnection;
import java.net.malformedurlexception;
import java.net.url;
import java.net.urlclassloader;
import java.util.jar.attributes;

/**
 * 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();
        }
        url url = null;
        try {
            url = new url(args[0]);
        } catch (malformedurlexception e) {
            fatal("invalid url: " + args[0]);
        }
        // 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.printstacktrace();
            system.exit(1);
        }
        if (name == null) {
            fatal("specified jar file does not contain a 'main-class'"
                    + " manifest attribute");
        }
        // 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);
        } catch (nosuchmethodexception e) {
            fatal("class does not define a 'main' method: " + name);
        } catch (invocationtargetexception e) {
            e.gettargetexception().printstacktrace();
            system.exit(1);
        }
    }
   
    private static void fatal(string s) {
        system.err.println(s);
        system.exit(1);
    }
   
    private static void usage() {
        fatal("usage: java jarrunner url [args..]");
    }
}

/**
 * a class loader for loading jar files, both local and remote.
 */

class jarclassloader extends urlclassloader {
    private url url;
   
    /**
     * creates a new jarclassloader for the specified url.
     *
     * @param url
     *            the url of the jar file
     */
    public jarclassloader(url url) {
        super(new url[] { url });
        this.url = url;
    }
   
    /**
     * returns the name of the jar file main class, or null if no "main-class"
     * manifest attributes was defined.
     */
    public string getmainclassname() throws ioexception {
        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;
    }
   
    /**
     * 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".
     *
     * @param name
     *            the name of the main class
     * @param args
     *            the arguments for the application
     * @exception classnotfoundexception
     *                if the specified class could not be found
     * @exception nosuchmethodexception
     *                if the specified class does not contain a "main" method
     * @exception invocationtargetexception
     *                if the application raised an exception
     */
    public void invokeclass(string name, string[] args)
    throws classnotfoundexception, nosuchmethodexception,
            invocationtargetexception {
        class c = loadclass(name);
        method m = c.getmethod("main", new class[] { args.getclass() });
        m.setaccessible(true);
        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
        }
    }
   
}

扫描关注微信公众号