2015-10-23 11 views
0

我想讀取從我的項目的根目錄可訪問的文件夾中的jar的內容,該jar是正確找到,但是,我的代碼只打印名稱在META-INF文件,這裏是我試過至今:只有meta/inf文件可用於探索jar內容

public static void provideClassList(String jarName) { 


    List<String> classNames = new ArrayList<String>(); 
    ZipInputStream zip; 
    try { 
     zip = new ZipInputStream(new FileInputStream(StaticValues.JARS_PATH.concat(jarName))); 
     for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { 
      System.out.println(entry);//PRINTS META-INF/ 
      if (!entry.isDirectory() && entry.getName().endsWith(".class")) { 
       String className = entry.getName().replace('/', '.'); 
       classNames.add(className.substring(0, className.length() - ".class".length())); 
      } 
      zip.close(); 
     } 

     // explore content (THIS IS ACTUALLY EMPTY) 
     for (String className : classNames) { 
      try { 
       Class<?> clazz = Class.forName(className); 
       System.out.println(clazz.getCanonicalName()); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } 
    } catch (FileNotFoundException e1) { 
     e1.printStackTrace(); 
    } catch (IOException ex) { 

    } 
} 

我看不到任何的權限管理問題,另外,我手動從控制檯打開JAR文件,並且我希望找到的內容都在那裏。 這些都是我從月食看到屬性: enter image description here

+0

不知道你在做什麼,但如果你只是想列出所有的文件,不需要ZipInputStream(實際上是通過整個文件)。改用'ZipFile'。 –

+0

@CarstenHoffmann我需要實例化包含在我的構建路徑中的jar中的每個類的對象,並調用它的一個方法,將嘗試ZipFile,thx – JBoy

+0

嗨,我是問題的所有者,你可以看問題的解決方案是不是真的與這個問題有關,請你關閉嗎?因爲這可能會誤導其他用戶,並且因爲它沒用,它只會佔用db中的空間。012xthx – JBoy

回答

1

要調用zip.close();的for循環內部,即propably你只能得到罐中的第一個條目的原因。將它移到for循環之外,或者甚至更好地使用try-with-resources語句。

try (FileInputStream fis = new FileInputStream(StaticValues.JARS_PATH.concat(jarName); 
    ZipInputStream zip = new ZipInputStream(fis)) { 
    // code for iterating goes here 
} 
+0

thx,我真的沒有看到 – JBoy