栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在当前JAR文件的目录中列出文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在当前JAR文件的目录中列出文件

旧的Java1.4代码,但这会让您有一个主意:

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();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463737.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号