有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

    }
  }
}

共 (1) 个答案

  1. # 1 楼答案

    不能从任意URL创建文件对象,也不能使用通常的文件系统遍历方法。现在,我不确定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文件中的所有条目,即文件和目录