WEB开发网
开发学院WEB开发Jsp 从任意位置运行Jar文件 阅读

从任意位置运行Jar文件

 2008-01-05 08:58:29 来源:WEB开发网   
核心提示:/* From http://java.sun.com/docs/books/tutorial/index.Html */import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.refl

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


Tags:任意 位置 运行

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接