获取 Java 小程序 JAR 内的文件列表
我正在尝试使用一种显然适用于非 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
谁首先把它们放在那里?如果答案是“我们做到了”,那么解决方案很简单。
images/imagelist.txt
)包含图像路径/名称列表。getResource(String)
获取对列表的引用。好的。如果您可以为 Zip 文件形成一个 URL(从而形成一个
InputStream
),则可以建立一个ZipInputStream
来自它。无论 URL 是缓存在本地文件系统上的 Zip/Jar 还是仍然在服务器上,这都应该有效。ZipInputStream
。null
ArrayList
中。当然,您仍然需要签名&调用保护域的可信代码。
要获取 Jar 的 URL,请尝试此操作(未经测试)。我们假设小程序是
com.our.BookingApplet
。获取类所在 Jar 的 URL
URL urlToApplet = this.getClass().getResource("/com/our/BookingApplet.class")
String[] parts = urlToApplet.toString().split("!")< /code> 将提供两部分,第一部分是 Jar URL 的
String
表示形式。String
建立URL
,然后按照之前更新中的说明使用该URL
。Who put them in there in the first place? If the answer is 'we did', the solution is simple.
images/imagelist.txt
) in the Jar.getResource(String)
.OK. If you can form an URL to (and thereby an
InputStream
from) the Zip file, it is possible to establish aZipInputStream
from it. This should work whether the URL is to a Zip/Jar cached on the local file-system or still at the server.ZipInputStream
from the URL.getNextEntry()
untilnull
ArrayList
.Of course, you'll still need signed & trusted code to call for the protection domain.
To get an URL to the Jar, try this (untested). Let's assume the applet is
com.our.BookingApplet
.URL urlToApplet = this.getClass().getResource("/com/our/BookingApplet.class")
String[] parts = urlToApplet.toString().split("!")
will provide two parts, the first will be aString
representation of the Jar URL.String
to establish anURL
, then use theURL
as described in the previous update.感谢 Andrew Thompson 的出色的回答!绝对赞成他的答案,而不是(或补充)这个答案,因为没有他的帮助,我将无法弄清楚这一点。
以下是我想出的用于获取 JAR 文件内的
.jpg
和.png
文件列表的代码部分。请注意,您可能需要更改存储MemoryGame
类的包的名称,以及更改类本身的名称。如果您想知道,
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 whereMemoryGame
class is stored, as well as change the name of the class itself.In case you wonder,
slike
meansimages
in Croatian since a lot of variables named in the exercise specification are in Croatian.仅允许“签名”小程序访问文件系统。如果您正在读取的 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.