2011-01-06 32 views
1

鑑於由多個jar組成的webstart應用程序,我如何列出這些jar包含的文件? (在運行時)列出了webstarted應用程序jar中的所有文件

由於提前,

阿爾諾

編輯:

的問題,下面mentionned的方法(這是非常相似,我一直使用到現在),是不知何故調用webstart時類路徑會發生變化。事實上,它不再引用你的罐子,而是一個deploy.jar

因此,如果您運行java -cp myjars test.ListMyEntries它將正確打印您的罐子的內容。另一方面,通過webstart,您將獲得deploy.jar的內容,因爲這是在webstarted時如何定義類路徑。我沒有在任何系統/部署屬性中找到任何原始jar名稱的蹤跡。

輸出樣本:

Entries of jar file /usr/lib/jvm/java-6-sun-1.6.0.06/jre/lib/deploy.jar 
META-INF/ 
META-INF/MANIFEST.MF 
com/sun/deploy/ 
com/sun/deploy/ClientContainer.class 
com/sun/deploy/util/ 
com/sun/deploy/util/Trace$TraceMsgQueueChecker.class 
+0

列出它們在哪裏? – jzd 2011-01-06 15:46:17

回答

0

如果我們有這些罐子(我認爲我們將不得不),那麼JDK的JarFile類可用於打開和遍歷一個jar文件的內容的磁盤訪問。枚舉中由條目方法返回的每個條目都是jar中的類名。

+1

主要的問題是,對於Java Web Start,他並不決定在哪裏以及如何將jar文件存儲在磁盤上。 – Gnoupi 2011-01-06 15:57:52

2

當然可以。但你應該簽署該類所在的罐子,並給予所有權限..

static void displayJarFilesEntries(){ 
    String cp = System.getProperty("java.class.path"); 
    String pathSep = File.pathSeperator; 
    String[] jarOrDirectories = cp.split(pathSep); 
    for(String fileName : jarOrDirectories){ 
     File file = new File(fileName); 
     if(file.isFile()){ 
      JarFile jarFile; 
      try{ 
       jarFile = new JarFile(fileName); 
      } catch(final IOException e){ 
       throw new RuntimeException(e); 
      } 
      System.out.println(" Entries of jar file " + jarFile.getName()); 
      for(final Enumeration<JarEntry> enumJar = jarFile.entries(); enumJar 
       .hasMoreElements();){ 
       JarEntry entry = enumJar.nextElement(); 
       System.out.println(entry.getName()); 
      } 
     } 
    } 
} 
+0

PS:你應該使用`String pathSep = System.getProperty(「path.separator」); String [] jarOrDirectories = cp.split(pathSep);`因爲分隔符可能會因平臺而有所不同。 – dagnelies 2011-01-06 16:47:02

0

您是否嘗試過使用

System.getProperty("java.class.path"); 

或者,你可以使用JMX:

RuntimeMXBean bean = /* This one is provided as default in Java 6 */; 
bean.getClassPath(); 
0

這是我做的:

public static List<String> listResourceFiles(ProtectionDomain protectionDomain, String endsWith) throws IOException 
{ 
    List<String> resources = new ArrayList<>(); 

    URL jar = protectionDomain.getCodeSource().getLocation(); 
    ZipInputStream zip = new ZipInputStream(jar.openStream()); 

    while(true) 
    { 
     ZipEntry e = zip.getNextEntry(); 
     if(e == null) break; 
     String name = e.getName(); 

     if(name.endsWith(endsWith)) resources.add(name); 
    } 

    return resources; 
} 
List<String> workflowFilePaths = AppUtils.listResourceFiles(getClass().getProtectionDomain(), ".bpmn20.xml"); 
相關問題