服务热线:13616026886

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

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

jar文件高级应用指南

    在应用程序中处理jar文件简单介绍了如何使用java.util.jar包提供的api操作jar文件,下面通过一个相对复杂的例子讲述一些jar文件相关的高级应用。仔细读读这篇文章并参考一下相关的java doc会对你学习java语言有很大的帮助。

    下面的应用程序将实现从http服务器装载并执行一个jar文件的功能,比如你的jar文件的地址为hello.jar。要实现这个功能我们应该首先建立与这个文件的连接然后通过manifest的信息描述得到main-class的值,最后装载并运行这个class。这里面需要用到java.net和反射的一些重要知识。这个应用程序由两个类组成:jarclassloader和jarrunner。

    jarclassloader扩展了urlclassloader,它有一个成员为url类型的url变量。
  public jarclassloader(url url)
  {
     super(new url[] { url });
      this.url = url;
  }
它的两个重要方法是getmainclassname()和invokeclass(),其中前者的目的是通过url和jar取得连接后,读取manifest的main-class属性从而得到应用程序的入点,这非常重要。得到入点后我们就可以通过反射机制装载和运行得到的主类。
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;
}

  public void invokeclass(string name, string[] args)
 throws classnotfoundexception,
        nosuchmethodexception,
        invocationtargetexception
    {
 class c = this.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
 }
 }
    url u = new url("jar", "", url + "!/");
    jarurlconnection uc = (jarurlconnection)u.openconnection();
这两段代码构造一个jarurlconnection的实例,注意!/的分隔符的意思是这个url表示的是整个jar文件。这样我们就建立了和jar文件的通信。方法中的后面两句话得到jar文件的主类。在invokeclass方法中,我们首先通过classloader的方法得到包括程序入口的主类,然后得到main方法,判断main方法为我们需要的方法后则调method的invoke方法执行这个应用程序。

    下面是源程序的代码
import java.net.url;
import java.net.urlclassloader;
import java.net.jarurlconnection;
import java.lang.reflect.method;
import java.lang.reflect.modifier;
import java.lang.reflect.invocationtargetexception;
import java.util.jar.attributes;
import java.io.ioexception;

class jarclassloader extends urlclassloader {
    private url url;
    public jarclassloader(url url) {
 super(new url[] { url });
 this.url = url;
    }

    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;

    }

    public void invokeclass(string name, string[] args)
 throws classnotfoundexception,
        nosuchmethodexception,
        invocationtargetexception
    {
 class c = this.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
 }
    }

}

import java.io.ioexception;
import java.net.url;
import java.net.malformedurlexception;
import java.lang.reflect.invocationtargetexception;

/**
 * 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..]");
    }
}
我们编写一个简单的helloworld程序,然后打个jar包,注意你的jar包内的manifest文件一定要包括main-class: helloworld,否则的话找不到程序的入口。把它放在一个web服务器上比如http://localhost/hello.jar。编译源程序后执行
java jarrunner http://localhost/hello.jar (可以含有参数)在控制台我们会看到hello world的字样输出!

扫描关注微信公众号