下面的應用程序將實(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的字樣輸出!
聯(lián)系客服