是否可以从特定路径{jar}中读取jar文件中的类文件

时间:2016-08-29 16:23:21

标签: java

所以我对java很新,但我想要实现的是我有一个列表,其中包含某个目录中的jar文件,我希望能够读取这些jar中的类文件然后全局搜索将在类文件中的字符串。

我目前的代码:

public class Dank 
{

    public static void main(String[] args) 
    {
        List<File> jarFiles = new ArrayList<>();
        File[] mods = new File(System.getProperty("user.home") + "\\AppData\\Roaming\\.minecraft\\mods").listFiles();


        for(File file : mods)
        {
            if(file.isFile())
            {
                String filename = file.getName();
                if(filename.endsWith(".jar")) {
                    jarFiles.add(file);
                //  System.out.println(filename);
                }
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

如果您正在寻找具有特定名称的变量,您可以使用以下内容:

Class<?> clazz; //get the class you need
Field f = clazz.getField("Variable_name");
// if Field f is null, no field exist with the given name

// this is necessary if the field is not public.
f.setAccessible(flag);

// if the field is not static, the parameter is the instance you want the field from
// otherwise it can be null
Object variableValue = f.get(null); // or f.get(clazzInstance);

这不需要特殊的类加载或类文件读取器/解析器。此外,它可以在任何时刻完成,而不会有太多的性能损失。

虽然作为免责声明,这确实比直接直接攻击字段需要更多时间。特别是因为您可以访问McForge,您也可以使用Optional的不同子类,这可能是解决此问题的更好解决方案。它记录得很好,所以我怀疑它不会太难掌握。

希望这指出你正确的方向,注意这篇文章实际上属于ForgeForums,因为它可以很容易地用McForge的系统解决。

相关问题