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

在当前JAR文件的目录中列出文件,第1张

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5439233.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存