[java]代码库
import java.io.*;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.jar.*;
public class JarDir {
public static void main(String args[])
throws IOException, ClassNotFoundException {
/*
* if (args.length != 1) {
*
* System.out.println("Please provide a JAR filename");
*
* System.exit(-1);
*
* }
*/
JarFile jarFile = new JarFile(
"D:\\cyproject\\fileOption\\WebRoot\\WEB-INF\\lib\\bsh.jar");
Enumeration enumjar = jarFile.entries();
while (enumjar.hasMoreElements()) {
process(enumjar.nextElement());
}
}
private static String returnTypeName(String cname) {
String classname = cname.substring(cname.lastIndexOf("."),
cname.length());
return classname;
}
private static void process(Object obj) throws ClassNotFoundException,
MalformedURLException {
JarEntry entry = (JarEntry) obj;
String name = entry.getName();
String classname = name.replace("/", ".");
String typename = returnTypeName(classname);
classname = classname.substring(0, classname.lastIndexOf("."));
File file = new File(
"D:\\cyproject\\fileOption\\WebRoot\\WEB-INF\\lib\\bsh.jar");
URL url = file.toURL();
URL[] urls = { url };
if (typename != null && typename.equals("") == false
&& (typename.equals(".class") || (typename.equals(".CLASS")))) {
// 实例化一个对象
// loader.loadClass(classpath).newInstance();
System.out.println();
System.out.println("==================this is The " + classname
+ "'s methods====================");
if (classname.equals("bsh.util.BeanShellBSFEngine")) {
ClassLoader loader = new URLClassLoader(urls);
Class cla = loader.loadClass(classname);
Method[] method = cla.getDeclaredMethods();
if (method.length > 0) {
for (int j = 0; j < method.length; j++) {
System.out.println(method[j].toString());
}
}
} else {
ClassLoader loader = new URLClassLoader(urls);
Class cla = loader.loadClass(classname);
Method[] method = cla.getDeclaredMethods();
if (method.length > 0) {
for (int j = 0; j < method.length; j++) {
System.out.println(method[j].toString());
}
}
}
}
// System.out.println("The type of class is :"+typename);
long size = entry.getSize();
long compressedSize = entry.getCompressedSize();
// System.out.println(classname + "\t" + size + "\t" + compressedSize);
}
}