国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

從任意位置運(yùn)行Jar文件

2019-11-18 14:48:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

/* 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.            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..]");
    }
}



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新津县| 枞阳县| 衡南县| 无棣县| 海林市| 沈丘县| 开原市| 鲁山县| 金塔县| 通辽市| 仪征市| 阿鲁科尔沁旗| 翁牛特旗| 邻水| 威宁| 思茅市| 瑞丽市| 凤城市| 沙田区| 苍溪县| 长乐市| 靖西县| 北安市| 湘潭县| 疏勒县| 武平县| 璧山县| 阿合奇县| 德安县| 淳化县| 乌苏市| 天津市| 通州区| 同心县| 墨竹工卡县| 政和县| 应用必备| 仙居县| 德令哈市| 临高县| 定结县|