[java] .jar's and loading images: I have no idea

Started by
4 comments, last by Son of Cain 18 years, 2 months ago
I'm sure this question is asked alot. I've been googling all over the place trying to figure it out but I can't figure it out. So anyway... I have made my game into a .jar file. I made my game using JBuilder, and everything was just peachy until I tried to put my .jar on my site. No matter what I do, I get a AccessControlException: access denied when I try and load images. I've tried a few different things, putting images in the jar, putting them outside the jar, using getClass().getResource(), using getImage(). I read a bunch of pages on this issue, but to no avail. Basically, I've been stuck for a long time, I need some advice, please.
Advertisement
Is it an applet ?
You may want to provide a policy to get rid of the AccessControlException.
Some interesting reading is here:
http://java.sun.com/j2se/1.4.2/docs/guide/security/PolicyFiles.html
http://java.sun.com/j2se/1.4.2/docs/guide/security/spec/security-specTOC.fm.html
I'm sorry. Yes, it is an applet, I'm not using java web start or anything. I'm looking at these sites that you posted, but as of right now I don't see how this really helps my situation.
try this:
Image image = (new javax.swing.ImageIcon(this.getClass().getResource(filepath))).getImage();

it's an old line in an old code of mine... i hope it's a working one ;)
The problem lies in trying to access resources (obviously) in an applet. Java is designed so that some malicious, or even innocent, programmer can't access your hard drive and say...delete your system folder.

I personally have had a similar problem with a data file, not images, but this site has some helpful information about security issues with applets. Hopefully that helps some.
Put your resources at the src folder of your applet (that is, immediately outside the packages' folders), and load your resources like this:

URL url = MyClass.class.getResource("/myImage.png"); // Dont't forget the slash besides the filename//Use the URL to load your resource. You can also use getResourceAsStream()


I believe this might work, I don't have the time to test it right now, so if anyone can prove me wrong, correct me please >_<

Son Of Cain

[edit: BTW, I mean to do that over a jar file]
a.k.a javabeats at yahoo.ca

This topic is closed to new replies.

Advertisement