Results 1 to 7 of 7
  1. #1
    kedinik is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Security Exception

    Hello,

    I built an applet in Eclipse that loads and draws several images from the same directory as the main .class file.

    It works just fine in Eclipse's appletviewer, but it looks my applet lacks permission to read the images in-browser. This causes an error that crashes my applet when embedded in Firefox, Chrome, etc.

    I do not understand how this is possible, because the image exists in the same directory as the applet and the .html file. According to my research, applets always have access to the contents of their own directory and sub-directories.

    What could possibly be causing this confusing error and how can I fix it?

    I've included the error message below, thank you.

    Java Code:
    java.lang.reflect.InvocationTargetException
    	at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: java.security.AccessControlException: access denied (java.io.FilePermission green_dot.png 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 java.io.File.canRead(Unknown Source)
    	at javax.imageio.ImageIO.read(Unknown Source)
    	at gamelogic.TailManager.<init>(TailManager.java:24)
    	at gameobjects.Soldier.<init>(Soldier.java:32)
    	at Seasnake.<init>(Seasnake.java:50)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    	at java.lang.reflect.Constructor.newInstance(Unknown Source)
    	at java.lang.Class.newInstance0(Unknown Source)
    	at java.lang.Class.newInstance(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception: java.lang.reflect.InvocationTargetException
    Last edited by kedinik; 02-09-2011 at 01:28 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    What could possibly be causing this confusing error and how can I fix it?
    This is the default security of an Applet. Sign the applet to provide the security rights to do what you want to do.

  3. #3
    kedinik is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Pardon my inexperience, but your advice conflicts with Oracle's description of Java's security system.

    All applets and any applications invoked with a security manager must be granted explicit permission to access local system resources apart from read access to the directory and its subdirectories where the program is invoked.

    That in mind, why would Java ever default to denying read access to an applet's own directory and its subdirectories? Am I misreading this? Is there a special case where this becomes incorrect?

    I had this same applet running in-browser as desired a few days ago. I don't know what has changed, but I did not need to sign the applet to load the images back then.

    To clarify, my in-browser tests are offline, not on a server.
    Last edited by kedinik; 02-09-2011 at 02:46 AM.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Sign the applet and see...should be the best test to solve your problem and determine for yourself what the security permissions are for an applet.
    What Applets Can and Cannot Do (The Java™ Tutorials > Deployment > Applets)
    OTN Discussion Forums : How to sign an applet (and get it to ...

  5. #5
    kedinik is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    I know that signing the applet will solve the problem and I appreciate your advice.

    At the same time, I've seen literally thousands of unsigned applets that load images from their own subdirectories without throwing security errors. I'm using one of them in another tab as I type this post.

    Signing strikes me as a messy work around. Potential users will have to wonder why a simple applet wants what-should-be-unnecessary access to their hard drive.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Then post code that demos the problem. We can go back and forth at this, but it will go nowhere. Posting code will clarify if your applet - regardless of the clause you posted above - attempts to load files using methods that are not allowed without the applet being signed.

    I've seen literally thousands of unsigned applets that load images from their own subdirectories without throwing security errors
    Thousands eh? The ones I've seen load resource files just as that: resources packaged within the jar itself. But I don't really make it a habit of studying applets and I can't claim to have seen (let alone studied the layout) of thousands.

  7. #7
    kedinik is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    I'm not doubting that your advice is correct and that I'm just misunderstanding something. I'm just hopeful to find a way to load images in an unsigned applet.

    So the problem might be with the method I'm using to load images and the fact that I'm not packaging the applet in a .jar?

    Here's some sample code, where type is a string that determines which image to load and images are stored in a sub-folder of the same name.

    Java Code:
    File file;
    		
    if(type == "green") {
    	file = new File("images/green_dot.png");
    }
    else {
    	file = new File("images/red_dot.png");
    }
    		
    try {
    	image = ImageIO.read(file);
    }
    catch (IOException e) {
               System.out.println(e);
    }

Similar Threads

  1. security
    By danghieu in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 08:24 PM
  2. Security Exception when Packages
    By kubas in forum Advanced Java
    Replies: 1
    Last Post: 02-11-2010, 08:21 PM
  3. Security
    By manticohd in forum Reviews / Advertising
    Replies: 1
    Last Post: 02-03-2010, 09:17 AM
  4. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  5. Replies: 1
    Last Post: 07-24-2007, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •