欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Jar文件高級應用指南
在應用程序中處理Jar文件簡(jiǎn)單介紹了如何使用java.util.jar包提供的API操作jar文件,下面通過(guò)一個(gè)相對復雜的例子講述一些Jar文件相關(guān)的高級應用。仔細讀讀這篇文章并參考一下相關(guān)的java doc會(huì )對你學(xué)習java語(yǔ)言有很大的幫助。

    下面的應用程序將實(shí)現從http服務(wù)器裝載并執行一個(gè)jar文件的功能,比如你的Jar文件的地址為hello.jar。要實(shí)現這個(gè)功能我們應該首先建立與這個(gè)文件的連接然后通過(guò)MANIFEST的信息描述得到Main-Class的值,最后裝載并運行這個(gè)class。這里面需要用到j(luò )ava.net和反射的一些重要知識。這個(gè)應用程序由兩個(gè)類(lèi)組成:JarClassLoader和JarRunner。

    JarClassLoader擴展了URLClassLoader,它有一個(gè)成員為URL類(lèi)型的url變量。
  public JarClassLoader(URL url)
  {
     super(new URL[] { url });
      this.url = url;
  }
它的兩個(gè)重要方法是getMainClassName()和invokeClass(),其中前者的目的是通過(guò)URL和jar取得連接后,讀取MANIFEST的Main-Class屬性從而得到應用程序的入點(diǎn),這非常重要。得到入點(diǎn)后我們就可以通過(guò)反射機制裝載和運行得到的主類(lèi)。
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();
這兩段代碼構造一個(gè)JarURLConnection的實(shí)例,注意!/的分隔符的意思是這個(gè)url表示的是整個(gè)jar文件。這樣我們就建立了和jar文件的通信。方法中的后面兩句話(huà)得到j(luò )ar文件的主類(lèi)。在invokeClass方法中,我們首先通過(guò)ClassLoader的方法得到包括程序入口的主類(lèi),然后得到main方法,判斷main方法為我們需要的方法后則調Method的invoke方法執行這個(gè)應用程序。

    下面是源程序的代碼
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..]");
    }
}
我們編寫(xiě)一個(gè)簡(jiǎn)單的HelloWorld程序,然后打個(gè)jar包,注意你的jar包內的MANIFEST文件一定要包括Main-Class: HelloWorld,否則的話(huà)找不到程序的入口。把它放在一個(gè)web服務(wù)器上比如http://localhost/hello.jar。編譯源程序后執行
java JarRunner http://localhost/hello.jar (可以含有參數)在控制臺我們會(huì )看到hello world的字樣輸出!

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
spring boot啟動(dòng)分析
使用URL存取jar文件的例子
在Java中調用matlab函數
學(xué)習反射
用Java實(shí)現JVM第一章《命令行工具》
在運行期動(dòng)態(tài)加載jar包/class目錄
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久