Jump to content

Permissions problem while accessing an image

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Hi again all, sorry to be such a nuisance.

I'm having problems accessing an image. I'm trying to put a simple, static image on to a Jlabel, and on to a JPanel. Here's the code:

ImageIcon image = new ImageIcon("CPA29.jpg");

	JLabel imageLabel = new JLabel(image);

	imagePane.add(imageLabel);

...with imagePane being a JPanel used to hold the JLabel holding the image.

When trying to run this, I get:

Java Console said:

java.security.AccessControlException: access denied (java.io.FilePermission CPA29.jpg read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
at sun.awt.SunToolkit.getImage(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at javax.swing.ImageIcon.<init>(Unknown Source)
at tpstart.init(tpstart.java:63)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.security.AccessControlException: access denied (java.io.FilePermission CPA29.jpg read)

I find this very odd; the image is in the same directory as the applet class (As you can see from my CreateImageIcon constructor call). I looked up the AccessControlException on the Sun Java website, and the first thing I saw was this:

Sun said:


(Quoted text links to reference)

...leading me to OMGWTF mode. It later goes on to explain that you can set an individual permission file in the user directory to allow certain permissions, but I'm hesitant to do this, as it's a bit much to do that for every user just for a simple applet, and according to the appendix, it should work anyways.

My only thought is that this work computer that I'm using has just been rebuilt by our IT support company and is used as a standalone machine, and I may not have admin permissions. But surely, that shouldn't be a problem? After all, non admins need to see applets too!

Anyone have any ideas of what I'm doing wrong?

Please and thank you, once again

~Fae

EDIT: Turns out I am a local admin. There's that theory out the window.

Edited by Fae, 23 August 2010 - 03:17 AM.

I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)

#2
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Bump. Anyone?

My latest thought is to compile it all and make it a .jar file for testing (I've never done this before, but could use the experience), as there's no way it won't have access to a file that contains everything relevant to the program.
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)

#3
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
try this:

Image image = getImage(getDocumentBase(), "CPA29.jpg");

ImageIcon icon = new ImageIcon(image);

JLabel imageLabel = new JLabel(icon);

I believe that if you don't use something like getDocumentBase() or this.getClass().getResource(), Java thinks you are trying to reach for a file outside of the applet's sandbox to the local file system, which is why it then throws the access exception.

#4
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
It may be an unsigned applet.
If so go to this microsoft link.
This should help you.

Edited by mr mike, 30 August 2010 - 06:35 PM.
typo, had Is wanted It


#5
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Thanks for all the help, everyone. Sorry it's taken a while for me to get back on this; this project is seciondary to my website design work, and so I don't work on this with any kind of frequency.

@Farrel2k
Brilliant, this worked perfectly, thanks. Although, I'm not quite sure why it works. As far as I can tell from looking it up, getDocumentBase() points to the HTML file that called the applet, and getCodeBase() returns the location of the .class file. However, from what I understand from the getImage() method, it's first parameter is a URL specifying the location of the file defined in the second parameter. However, I don't quite understand how using getDocumentBase() fills this criterion, as it will point to the HTML file in question, not the directory that the HTML file is in. I would have thought that this would return an error?

@Mr. Mike
Yeah, this is the first applet I've ever created... I honestly contemplated security issues such as this as I was starting the project, but didn't know for sure what would happen until I tried it. But from what I can gather from this, it uses a system similar to PowerShell's permissions system for running unsigned applets locally. How do I sign an applet? From what I've gathered so far, a business buys... something... that can digitally sign pieces of software, but I'm honestly not sure how the entire process works. Also, is signing a piece of software such as an applet similar in any way to security certificates (like on servers, etc...)? Broad questions, I know and maybe a little off topic, but still, my curiosity is piqued :)

Thanks again
~Fae
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)