Listing the files in a directory of the current JAR file

后端 未结 5 1808
轮回少年
轮回少年 2020-12-16 15:15

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

5条回答
  •  太阳男子
    2020-12-16 15:45

    10 years after the question, I propose an another way to do the job:

    private static void listFilesFromDirectoryInsideAJar(String pathToJar,String directory,String extension) {
            try {
                JarFile jarFile = new JarFile(pathToJar);
                Enumeration e = jarFile.entries();
                while (e.hasMoreElements()) {
                    JarEntry candidat = e.nextElement();
                    if (candidat.getName().startsWith(directory) && 
                        candidat.getName().endsWith(extension))
                        LOG.info(candidat.getName());
                }
            } catch (IOException e) {
                LOG.error(e.getMessage(),e);
            }
        }
    

提交回复
热议问题