直接遍历JAR文件中的文件夹



我有以下code来迭代类路径中的文件夹和文件,确定类,并获得一个带有ID的字段,并将它们打印到logger。如果我在IDE中运行这段代码,这可以很好地工作,但如果我用launch4j将我的项目打包到JAR文件中,并将该JAR文件打包到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)
      {
      }
    }
  }
}

您不能从任意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文件中的所有条目,即文件和目录。

相关内容

  • 没有找到相关文章

最新更新