2011-01-06 33 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"); 
相关问题