views:

497

answers:

3

I am having some issues with accessing files using JWS (Java Web Start). The program adds a new label and image. The program runs fine on my local computer but gives me pages of errors when I run the program on my remote server using JWS. Here's a sample of the error:

Exception in thread "AWT-EventQueue-0" java.security.AccessControlException: access denied (java.io.FilePermission add2.png read)
 at java.security.AccessControlContext.checkPermission(Unknown Source)
 at java.security.AccessController.checkPermission(Unknown Source)
 at java.lang.SecurityManager.checkPermission(Unknown Source)

This occurs even after making sure the images have read access.

Ideas?

+2  A: 

Where is add2.png? If it's on your local filesystem (as opposed to the server that has the .jnlp file), then this is not allowed, to protect the user's privacy. Also, getting the resource from another web server is not allowed either.

Remember that JWS and applet code is generally untrusted (unless it's been digitally signed, and accepted by the user). So, the default permissions applying to them have to be fairly restrictive.


Edited to add: It seems from the stacktrace that your program is still trying to read the local file, rather than using the URL to the remote server. Make sure your code does not make any references to java.io.File; this will help you pinpoint any problematic areas of code.

Chris Jester-Young
I've uploaded it to the server.
Miamian
The same server? Or a different one? Also, strip out all mentions of `File` from your code, if you haven't already done so.
Chris Jester-Young
+3  A: 

Like applets, JNLP (webstart) runs at client machine, not at server machine. The client downloads the program from a webpage and runs it at local machine. Any references in java.io stuff will point to the local disk file system (there where the code runs), not the remote disk file system (there where the code is been downloaded from) as you seem to expect.

You have 2 options:

  1. Pack the image in the JAR and use ClassLoader#getResourceAsStream() instead to obtain an InputStream from it.

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    BufferedImage image = ImageIO.read(classLoader.getResourceAsStream("add2.png"));
    
  2. Put the image in public location in the webserver, so that you can access it by URL (is only a tad slower than having in classpath).

    BufferedImage image = ImageIO.read(new URL("http://example.com/add2.png"));
    

That said, regardless of all, using relative paths instead of absolute paths in java.io stuff is a bad idea. Never do this. It will be dependent on the current working directory, which you have no control over.

BalusC