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 u开发者_如何学Gosing 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?
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:
Pack the image in the JAR and use
ClassLoader#getResourceAsStream()
instead to obtain anInputStream
from it.ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); BufferedImage image = ImageIO.read(classLoader.getResourceAsStream("add2.png"));
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.
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.
Your JAR must be signed. Read this tutorials:
http://www.mobilefish.com/tutorials/java/java_quickguide_keytool.html
http://www-personal.umich.edu/~lsiden/tutorials/signed-applet/signed-applet.html
http://faq.javaranch.com/java/HowCanAnAppletReadFilesOnTheLocalFileSystem
Faced similar issue.
Resolved by removing Temporary Internet Files from Control Panel -> Java
精彩评论