在本例中,我们将向你演示如何使用JarURLConnection类,这个类通过JAR协议建立出了一个jar URL的连接。
JarURLConnection的实例可以引用一个JAR的压缩包或者这种包里的某个文件。jar URL的形式如下:jar:{archive-url}!/{entry},其中!/是一个分隔符。这个分隔符是用来区分引用的究竟是一个归档文件还是里面的一个文件。
示例:
Jar包文件:jar:http://www.jcg.com/bar/baz.jar!/Jar包内的文件:jar:http://www.jcg.com/bar/baz.jar!/com/foo/Quux.classJar包内的目录:jar:http://www.jcg.com/bar/baz.jar!/com/foo/
现在,假设我们有一个main程序。同时,我们还有一个小程序在为主程序执行某些逻辑。那我们怎么才能在主程序中运行这个打包成了JAR文件的小应用呢?
好吧,我们来看下下面这个例子。
示例:
package com.jcg;
/**
* @author ashraf
*
*/
public class Bean {
public static void main( String [] args) {
sayHello();
}
public static void sayHello() {
System.out.println("Hello from loaded JAR class !!!");
}
}
JarURLConnectionTest.java:
package com.jcg;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map.Entry;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
/**
* @author ashraf
*
*/
public class JarURLConnectionTest {
private final static String JAR_URL = "jar:file:/C:/Users/ashraf_sarhan/simple-bean-1.0.jar!/";
private final static String JAR_FILE_PATH = "file:/C:/Users/ashraf_sarhan/simple-bean-1.0.jar";
private static URLClassLoader urlClassLoader;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {
// Create a URL that refers to a jar file in the file system
URL FileSysUrl = new URL(JAR_URL);
// Create a jar URL connection object
JarURLConnection jarURLConnection = (JarURLConnection)FileSysUrl.openConnection();
// Get the jar file
JarFile jarFile = jarURLConnection.getJarFile();
// Get jar file name
System.out.println("Jar Name: " + jarFile.getName());
// When no entry is specified on the URL, the entry name is null
System.out.println("\nJar Entry: " + jarURLConnection.getJarEntry());
// Get the manifest of the jar
Manifest manifest = jarFile.getManifest();
// Print the manifest attributes
System.out.println("\nManifest file attributes: ");
for (Entry entry : manifest.getMainAttributes().entrySet()) {
System.out.println(entry.getKey() +": "+ entry.getValue());
}
System.out.println("\nExternal JAR Execution output: ");
// Get the jar URL which contains target class
URL[] classLoaderUrls = new URL[]{new URL(JAR_FILE_PATH)};
// Create a classloader and load the entry point class
urlClassLoader = new URLClassLoader(classLoaderUrls);
// Get the main class name (the entry point class)
String mainClassName = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
// Load the target class
Class beanClass = urlClassLoader.loadClass(mainClassName);
// Get the main method from the loaded class and invoke it
Method method = beanClass.getMethod("main", String[].class);
// init params accordingly
String[] params = null;
// static method doesn't have an instance
method.invoke(null, (Object) params);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Jar Name: C:\Users\ashraf_sarhan\simple-bean-1.0.jar
Jar Entry: null
Manifest file attributes:
Build-Jdk: 1.7.0_40
Built-By: ashraf_sarhan
Manifest-Version: 1.0
Created-By: Apache
MavenMain-Class: com.jcg.Bean
Archiver-Version: Plexus ArchiverExternal JAR
Execution output:
Hello from loaded JAR class !!!
分析:在上述例子中,你可能并不知道要运行的JAR文件中哪个是主类,因此我们使用JarURLConnection加载了这个会打印一个消息到控制台上的simple-bean-1.0.jar 文件,并读取它的Manifest来查找出入口类(也就是主类的名字——com.jcg.Bean),然后创建了一个类加载器并进行加载。最后,我们通过反射来获取这个类的public static void main(String[])方法并进行调用。当通过new URL(context, spec)来构造JAR URL的时候,会遵循如下规则:- 如果没有context URL并且传到URL构造方法里的spec不包含分隔符的话,URL引用的是一个jar文件。- 如果存在context URL,那么该URL引用的就是一个JAR包里的文件或者目录。- 如果spec以/开头,则忽略该Jar的目录,spec路径从jar包的的根路径开始。例子:
context: jar:http://www.jcg.com/bar/jar.jar!/, spec: baz/entry.txturl: jar:http://www.jcg.com/bar/jar.jar!/baz/entry.txtcontext: jar:http://www.jcg.com/bar/jar.jar!/baz, spec: entry.txturl: jar:http://www.jcg.com/bar/jar.jar!/baz/entry.txt
context: jar:http://www.jcg.com/bar/jar.jar!/baz, spec: /entry.txturl: jar:http://www.jcg.com/bar/jar.jar!/entry.txt
转自:http://www.deepinmind.com/examples/java/2014/10/01/java-net-jarurlconnection-example.html