java 调用jar包 类_java类中如何读取引用jar包中目录中的所有资源文件?

你可以模仿一下,下面的代码private static List getClassesFromJARFile(String jar, String packageName) throws Error

{

final List classes = new ArrayList();

JarInputStream jarFile = null;

try

{

jarFile = new JarInputStream(new FileInputStream(jar));

JarEntry jarEntry;

do

{

try

{

jarEntry = jarFile.getNextJarEntry();

}

catch(IOException ioe)

{

throw new CCException.Error("Unable to get next jar entry from jar file '"+jar+"'", ioe);

}

if (jarEntry != null)

{

extractClassFromJar(jar, packageName, classes, jarEntry);

}

} while (jarEntry != null);

closeJarFile(jarFile);

}

catch(IOException ioe)

{

throw new CCException.Error("Unable to get Jar input stream from '"+jar+"'", ioe);

}

finally

{

closeJarFile(jarFile);

}

return classes;

}

private static void extractClassFromJar(final String jar, final String packageName, final List classes, JarEntry jarEntry) throws Error

{

String className = jarEntry.getName();

if (className.endsWith(".class"))

{

className = className.substring(0, className.length() - ".class".length());

if (className.startsWith(packageName))

{

try

{

classes.add(Class.forName(className.replace('/', '.')));

} catch (ClassNotFoundException cnfe)

{

throw new CCException.Error("unable to find class named " + className.replace('/', '.') + "' within jar '" + jar + "'", cnfe);

}

}

}

}

private static void closeJarFile(final JarInputStream jarFile)

{

if(jarFile != null)

{

try

{

jarFile.close();

}

catch(IOException ioe)

{

mockAction();

}

}

}

2012年6月20日 17:22

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值