列出当前JAR文件的目录中的文件 [英] Listing the files in a directory of the current JAR file

查看:156
本文介绍了列出当前JAR文件的目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JAVA制作一个游戏,我想在我的jar中的某个目录中找到一个文件列表,所以我可以确保有一个列表在游戏中使用的类。



例如在我的jar中有一个目录

  mtd / entity / creep / 

我想获取该目录中所有.class文件的列表 using



这是最好的代码是什么?

解决方案

旧的java1.4代码,但这将给你的想法:

  private static List getClassesFromJARFile(String jar,String packageName)throws错误
{
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);
}
返回类;
}
private static void extractClassFromJar(final String jar,final String packageName,final List classes,JarEntry jarEntry)throws错误
{
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(could to find class class+ className.replace('/','。')+ ''+ jar +',cnfe);
}
}
}
}
private static void closeJarFile(final JarInputStream jarFile)
{
if(jarFile!= null)
{
try
{
jarFile.close();
}
catch(IOException ioe)
{
mockAction();
}
}
}


I am making a game in JAVA where I want to come up with a list of files in a certain directory in my jar so I can make sure to have a list of those classes to be used in the game.

For example say in my jar I have a directory

mtd/entity/creep/

I want to get a list of all the .class files in that directory using java code from another class in the jar.

What is the best code to do so?

解决方案

Old java1.4 code, but that would give you the idea:

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

这篇关于列出当前JAR文件的目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆