JAR Document Advanced Application Guide

xiaoxiao2021-03-06  50

Handling JAR files in the application briefly describes how to use the Java.util.jar package API operation JAR file, below to tell the advanced applications related to some JAR files through a relatively complex example. Read this article carefully and refer to the relevant Java Doc will help you learn Java languages.

The following application will implement the function of loading and execute a JAR file from the HTTP server, such as the address of your JAR file is hello.jar. To implement this feature, we should first establish a connection with this file and then describe the value of Main-Class by using information by manifest, and finally loaded and running this class. This requires some important knowledge of java.net and reflection. This application consists of two classes: JarclassLoader and Jarrunner.

JarclassLoader extends URLClassLoader, which has a member of the URL type URL variable. Public JarclassLoader (URL URL) {Super (new url [] {url}; this.url = url;} It two important methods are getMainClassName () and invokeClass (), where the former is to get through URL and JAR After the connection, read the MAIN-CLASS attribute of Manifest to get the application's entry, which is very important. After getting the point, we can load and run the main class by reflective mechanism. 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; 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 stay NOT CHECKS}} URL U = New URL ("Jar", ", URL "! / "); JarurlConnection UC = (JarurlConnec TION) u.openConnection (); these two code construct a JarurLConnection instance, note! / Separator means that this URL represents the entire JAR file. This way we created communication with the JAR file. The latter words in the method get the main class of the JAR file. In the InvokeClass method, we first get the primary class including the program entry by the method of ClassLoad, and then get the main method, and determine that the main method is the method we need to perform this application.

Here is the code of the source program 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 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 (IllegalaccessExcection E) {// this stay not happen, as we have disabled access checks} }

} import java.io.ioException; import java.net.ur1; 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 * besed 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 (ooException e) {system.err.println ("I / O Error While Loading Jar File:"); E.PrintStackTrace (); System.exit (1 }} If ("" "" "} // Get Arguments for the application string [] new string [] new string [] new string [] new string Args.Length - 1]; System.ArrayCopy (Args, 1, New Args, 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 Defe);} catch (invocationtargetexce) {E.GETTARGETEXCEPTION (). PrintStackTrace (); system.exit (1);}} private static void Fatal (String s) {system.err.println (s); system.exit (1);

转载请注明原文地址:https://www.9cbs.com/read-116498.html

New Post(0)