Listing the files in a directory of the current JAR file

后端 未结 5 1807
轮回少年
轮回少年 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:38

    Remember that JAR files are just ZIP files renamed, and it's very easy to read the contents of ZIP files in Java:

        File jarName = null;
        try
        {
            jarName = new File (Dir.class.getProtectionDomain().getCodeSource().getLocation().toURI());
        }
        catch (Exception e)
        {
            e.printStackTrace();    
        }
    
        try 
        {
          ZipFile zf=new ZipFile(jarName.getAbsoluteFile());
          Enumeration e=zf.entries();
          while (e.hasMoreElements()) 
          {
              ZipEntry ze=(ZipEntry)e.nextElement();
              System.out.println(ze.getName());
          }
          zf.close();
       } catch (IOException e) 
       {
          e.printStackTrace();
       }
    

提交回复
热议问题