直接在JAR文件中迭代文件夹

时间:2013-07-02 09:08:11

标签: java file class reflection jar

我有以下code来迭代类路径中的文件夹和文件,并确定类并获取带有ID的字段并将其打印到logger。如果我在我的IDE中运行此代码,这工作正常,但如果我将项目打包到JAR文件并将此JAR文件打包到带有launch4j的EXE文件中,我就无法迭代我的类再次。 如果我尝试在JAR / EXE文件中迭代我的类,我会得到以下路径:

file:/C:/ENTWICKLUNG/java/workspaces/MyProject/MyProjectTest/MyProjectSNAPSHOT.exe!/com/abc/def

如何实现此操作以迭代我的JAR / EXE文件中的所有类?

public class ClassInfoAction extends AbstractAction
{
  /**
   * Revision/ID of this class from SVN/CVS.
   */
  public static String ID = "@(#) $Id ClassInfoAction.java 43506 2013-06-27 10:23:39Z $";

  private ClassLoader classLoader = ClassLoader.getSystemClassLoader();
  private ArrayList<String> classIds = new ArrayList<String>();
  private ArrayList<String> classes = new ArrayList<String>();
  private int countClasses = 0;

  @Override
  public void actionPerformed(ActionEvent e)
  {
    countClasses = 0;
    classIds = new ArrayList<String>();
    classes = new ArrayList<String>();

    getAllIds();

    Iterator<String> it = classIds.iterator();

    while (it.hasNext())
    {
      countClasses++;
      //here I print out the ID
    }
  }

  private void getAllIds()
  {
    String tempName;
    String tempAbsolutePath;

    try
    {
      ArrayList<File> fileList = new ArrayList<File>();
      Enumeration<URL> roots = ClassLoader.getSystemResources("com"); //it is a path like com/abc/def I won't do this path public
      while (roots.hasMoreElements())
      {
        URL temp = roots.nextElement();
        fileList.add(new File(temp.getPath()));
        GlobalVariables.LOGGING_logger.info(temp.getPath());
      }

      for (int i = 0; i < fileList.size(); i++)
      {
        for (File file : fileList.get(i).listFiles())
        {
          LinkedList<File> newFileList = null;
          if (file.isDirectory())
          {
            newFileList = (LinkedList<File>) FileUtils.listFiles(file, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);

            if (newFileList != null)
            {
              for (int j = 0; j < newFileList.size(); j++)
              {
                tempName = newFileList.get(j).getName();
                tempAbsolutePath = newFileList.get(j).getAbsolutePath();
                checkIDAndAdd(tempName, tempAbsolutePath);
              }
            }
          }
          else
          {
            tempName = file.getName();
            tempAbsolutePath = file.getAbsolutePath();
            checkIDAndAdd(tempName, tempAbsolutePath);
          }
        }
      }

      getIdsClasses();
    }
    catch (IOException e)
    {
    }
  }

  private void checkIDAndAdd(String name, String absolutePath)
  {
    if (name.endsWith(".class") && !name.matches(".*\\d.*") && !name.contains("$"))
    {
      String temp = absolutePath.replace("\\", ".");
      temp = temp.substring(temp.lastIndexOf(/* Class prefix */)); //here I put in the class prefix
      classes.add(FilenameUtils.removeExtension(temp));
    }
  }

  private void getIdsClasses()
  {
    for (int i = 0; i < classes.size(); i++)
    {
      String className = classes.get(i);

      Class<?> clazz = null;
      try
      {
        clazz = Class.forName(className);

        Field idField = clazz.getDeclaredField("ID");
        idField.setAccessible(true);

        classIds.add((String) idField.get(null));
      }
      catch (ClassNotFoundException e1)
      {
      }
      catch (NoSuchFieldException e)
      {
      }
      catch (SecurityException e)
      {
      }
      catch (IllegalArgumentException e)
      {
      }
      catch (IllegalAccessException e)
      {
      }

    }
  }
}

1 个答案:

答案 0 :(得分:4)

您无法从任意URL创建File对象并使用常用的文件系统遍历方法。现在,我不确定launch4j是否确实有所不同,但是对于普通JAR文件的内容进行迭代,您可以使用官方API:

JarURLConnection connection = (JarURLConnection) url.openConnection();
JarFile file = connection.getJarFile();
Enumeration<JarEntry> entries = file.entries();
while (entries.hasMoreElements()) {
    JarEntry e = entries.nextElement();
    if (e.getName().startsWith("com")) {
        // ...
    }
}

上面的代码段列出了url引用的JAR文件中的所有条目,即文件和目录。