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.
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
InputStreamfrom) the Zip file, it is possible to establish aZipInputStreamfrom it. This should work whether the URL is to a Zip/Jar cached on the local file-system or still at the server.ZipInputStreamfrom the URL.getNextEntry()untilnullArrayList.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 aStringrepresentation of the Jar URL.Stringto establish anURL, then use theURLas described in the previous update.