获取 Java 小程序 JAR 内的文件列表

发布于 2024-12-25 20:06:16 字数 1581 浏览 5 评论 0原文

我正在尝试使用一种显然适用于非 applet Java 代码的方法来获取文件列表。

我完全知道这很混乱;我只是想让这个用于学校作业。 (我不喜欢 Java。)

        CodeSource src = MemoryButtonHelper.class.getProtectionDomain().getCodeSource();
        if (src == null) {
            throw new Exception();
        }

        URL jar = src.getLocation();
        System.out.println("Loading from " + jar);

        JarFile zf=new JarFile(jar.toString()); //jar.openStream());

        final Enumeration<JarEntry> entries = zf.entries();

        while (entries.hasMoreElements()) {
            final JarEntry ze = entries.nextElement();

            if(ze.getName().endsWith(".jpg") || ze.getName().endsWith(".png"))
            {
                System.out.println("Adding " + ze.getName());
                slikeList.add(ze.getName());
            }
        }
        zf.close();

不幸的是,我遇到了安全异常。

java.security.AccessControlException: access denied (java.lang.RuntimePermission getProtectionDomain)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
    at java.security.AccessController.checkPermission(AccessController.java:546)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
    at java.lang.Class.getProtectionDomain(Class.java:2106)
    at hr.tvz.programiranje.java.deseti.helpers.MemoryButtonHelper.getSlike(MemoryButtonHelper.java:75)
        … ad nauseam … 

根据Java控制台,异常似乎发生在println("Loading from " + jar)之前。

这是一个奖励点分配,它特别指出我们必须从 JAR 文件中获取图像列表。由于这是我第一次接触小程序,因此我不确定如何获取图像列表。

I'm trying to fetch list of files using a method that apparently works well with non-applet Java code.

I'm fully aware it's messy; I'm just trying to get this to work for a school assignment. (I'm no fan of Java.)

        CodeSource src = MemoryButtonHelper.class.getProtectionDomain().getCodeSource();
        if (src == null) {
            throw new Exception();
        }

        URL jar = src.getLocation();
        System.out.println("Loading from " + jar);

        JarFile zf=new JarFile(jar.toString()); //jar.openStream());

        final Enumeration<JarEntry> entries = zf.entries();

        while (entries.hasMoreElements()) {
            final JarEntry ze = entries.nextElement();

            if(ze.getName().endsWith(".jpg") || ze.getName().endsWith(".png"))
            {
                System.out.println("Adding " + ze.getName());
                slikeList.add(ze.getName());
            }
        }
        zf.close();

Unfortunately, I'm getting a security exception.

java.security.AccessControlException: access denied (java.lang.RuntimePermission getProtectionDomain)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
    at java.security.AccessController.checkPermission(AccessController.java:546)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
    at java.lang.Class.getProtectionDomain(Class.java:2106)
    at hr.tvz.programiranje.java.deseti.helpers.MemoryButtonHelper.getSlike(MemoryButtonHelper.java:75)
        … ad nauseam … 

According to Java Console, exception appears to occur before the println("Loading from " + jar).

This is a bonus point assignment which specifically says that we must fetch the list of images from the JAR file. Since this is my first encounter with the applets, I'm not sure what I can do to fetch the list of images.

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

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

发布评论

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

评论(3

絕版丫頭 2025-01-01 20:06:16

..我们必须从 JAR 文件中获取图像列表。

谁首先把它们放在那里?如果答案是“我们做到了”,那么解决方案很简单。

  • 在 Jar 中的已知位置(例如 images/imagelist.txt)包含图像路径/名称列表。
  • 使用getResource(String) 获取对列表的引用。
  • 阅读列表(可能使用行阅读器)。

显然他们希望我按原样列出 jar 的内容,而不需要额外的元数据。

好的。如果您可以为 Zip 文件形成一个 URL(从而形成一个 InputStream),则可以建立一个 ZipInputStream 来自它。无论 URL 是缓存在本地文件系统上的 Zip/Jar 还是仍然在服务器上,这都应该有效。

  1. 获取 Jar 的 URL。
  2. 从 URL 建立 ZipInputStream
  3. 使用 < 迭代条目code>getNextEntry() 直到 null
  4. 检查每个条目是否存在潜在匹配项,如果存在,则将其添加到 ArrayList 中。

当然,您仍然需要签名&调用保护域的可信代码。


图像肯定在同一个 JAR 中

要获取 Jar 的 URL,请尝试此操作(未经测试)。我们假设小程序是com.our.BookingApplet

  1. 使用
    获取类所在 Jar 的 URL
    URL urlToApplet = this.getClass().getResource("/com/our/BookingApplet.class")
  2. String[] parts = urlToApplet.toString().split("!")< /code> 将提供两部分,第一部分是 Jar URL 的 String 表示形式。
  3. 使用该 String 建立 URL,然后按照之前更新中的说明使用该 URL

..we must fetch the list of images from the JAR file.

Who put them in there in the first place? If the answer is 'we did', the solution is simple.

  • Include a list of image path/names at a known location (e.g. images/imagelist.txt) in the Jar.
  • Obtain a reference to the list using getResource(String).
  • Read the list (probably using a line reader).

Apparently they want me to list the contents of the jar as-is, without extra metadata.

OK. If you can form an URL to (and thereby an InputStream from) the Zip file, it is possible to establish a ZipInputStream from it. This should work whether the URL is to a Zip/Jar cached on the local file-system or still at the server.

  1. Get an URL to the Jar.
  2. Establish a ZipInputStream from the URL.
  3. Iterate the entries using getNextEntry() until null
  4. Examine each one for a potential match and if it does, add it to an ArrayList.

Of course, you'll still need signed & trusted code to call for the protection domain.


Images are definitely in the same JAR

To get an URL to the Jar, try this (untested). Let's assume the applet is com.our.BookingApplet.

  1. Obtain an URL to the Jar in which the class resides, using
    URL urlToApplet = this.getClass().getResource("/com/our/BookingApplet.class")
  2. String[] parts = urlToApplet.toString().split("!") will provide two parts, the first will be a String representation of the Jar URL.
  3. Use that String to establish an URL, then use the URL as described in the previous update.
梦初启 2025-01-01 20:06:16

感谢 Andrew Thompson 的出色的回答!绝对赞成他的答案,而不是(或补充)这个答案,因为没有他的帮助,我将无法弄清楚这一点。

以下是我想出的用于获取 JAR 文件内的 .jpg.png 文件列表的代码部分。请注意,您可能需要更改存储 MemoryGame 类的包的名称,以及更改类本身的名称。

        List<String> slikeList = new ArrayList<String>();

        URL urlToApplet = MemoryGame.class.getResource("/com/whoever/whatever/gui/MemoryGame.class");
        String[] parts = urlToApplet.toString().split("!");
        String jarURLString = parts[0].replace("jar:", "");
        System.out.println("Loading from " + jarURLString);

        URL jar = new URL(jarURLString);

        URLConnection jarConnection = jar.openConnection();
        JarInputStream jis = new JarInputStream(jarConnection.getInputStream());

        JarEntry je = jis.getNextJarEntry();
        while(je != null)
        {
            System.out.println("Inspecting " + je);
            if(je.getName().endsWith(".jpg") || je.getName().endsWith(".png"))
            {
                System.out.println("Adding " + je.getName());
                slikeList.add(je.getName());
            }
            je = jis.getNextJarEntry();
        }

如果您想知道,slike 在克罗地亚语中意味着 images,因为练习规范中命名的许多变量都是克罗地亚语。

Thanks go to Andrew Thompson for his excellent answer! Definitely upvote his answer instead (or in addition) to this one, since without his help, I wouldn't be able to figure this out.

Here is the portion of the code which I came up with to use to fetch list of .jpg and .png files inside the JAR file. Note that you probably need to change the name of the package where MemoryGame class is stored, as well as change the name of the class itself.

        List<String> slikeList = new ArrayList<String>();

        URL urlToApplet = MemoryGame.class.getResource("/com/whoever/whatever/gui/MemoryGame.class");
        String[] parts = urlToApplet.toString().split("!");
        String jarURLString = parts[0].replace("jar:", "");
        System.out.println("Loading from " + jarURLString);

        URL jar = new URL(jarURLString);

        URLConnection jarConnection = jar.openConnection();
        JarInputStream jis = new JarInputStream(jarConnection.getInputStream());

        JarEntry je = jis.getNextJarEntry();
        while(je != null)
        {
            System.out.println("Inspecting " + je);
            if(je.getName().endsWith(".jpg") || je.getName().endsWith(".png"))
            {
                System.out.println("Adding " + je.getName());
                slikeList.add(je.getName());
            }
            je = jis.getNextJarEntry();
        }

In case you wonder, slike means images in Croatian since a lot of variables named in the exercise specification are in Croatian.

廻憶裏菂餘溫 2025-01-01 20:06:16

仅允许“签名”小程序访问文件系统。如果您正在读取的 jar 文件位于本地文件系统上,则需要先对小程序进行签名。

有关如何签署小程序的详细信息,请参阅链接。

Only "Signed" applets are allowed to access file system. If the jar file which you are reading from is located on your local file system, you will need to sign the applet first.

See this link for more information on how to sign applet.

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