列出 webstarted 应用程序的 jar 中的所有文件

发布于 2024-10-10 12:29:29 字数 679 浏览 4 评论 0原文

给定一个由多个 jar 组成的 webstart 应用程序,我如何列出这些 jar 中包含的文件? (在运行时)

提前致谢,

Arnaud

编辑:

下面提到的方法(与我到目前为止使用的非常相似)的问题是,在调用 webstart 时类路径会以某种方式发生变化。事实上,它不再引用您的 jar,而是引用一个 deploy.jar

因此,如果您运行 java -cp myjars test.ListMyEntries ,它将正确打印 jar 的内容。另一方面,通过 webstart,您将获得 deploy.jar 的内容,因为这是 webstart 时定义类路径的方式。我没有在任何系统/部署属性中找到任何原始 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

Given a webstart application composed of multiple jars, how could I list the files contained in these jars? (at runtime)

Thanks in advance,

Arnaud

EDIT:

The problem, with the method mentionned below (which is very similar to what I used until now), is that somehow the classpath changes when invoking webstart. Indeed, it doesn't reference your jars anymore but a deploy.jar instead.

As a consequence, if you run java -cp myjars test.ListMyEntries it will correctly print the content of your jars. On the other hand, via webstart, you will obtain the content of deploy.jar because this is how the classpath is defined when webstarted. I did not found any trace of the original jar names in any of the system/deployment properties.

Output sample:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情泪▽动烟 2024-10-17 12:29:29

是的,你可以..但是你应该对类所在的 jar 进行签名并授予所有权限..

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());
            }
        }
    }
}

Yes you can.. But you should sign the jar that class resides and give all permissions..

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());
            }
        }
    }
}
清风无影 2024-10-17 12:29:29

如果我们可以通过磁盘访问这些 jar(我认为我们应该有),那么 jdk 的 JarFile 类可用于打开和迭代 jar 文件的内容。 Entrys 方法返回的 Enumeration 中包含的每个条目都是 jar 中的类名。

If we have disk access to these jars (which i think we would have) then jdk's JarFile class can be used to open up and iterate over the contents of a jar file. Each entry contained in Enumeration returned by entries method is a class name within the jar.

尐籹人 2024-10-17 12:29:29

您是否尝试过使用

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

或者,您可以使用 JMX :

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

Have you tried using

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

Alternately, you can use JMX :

RuntimeMXBean bean = /* This one is provided as default in Java 6 */;
bean.getClassPath();
少女七分熟 2024-10-17 12:29:29

我就是这样做的:

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");

This is how I did it:

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文