AccessControlException with applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 40

Thread: AccessControlException with applet

  1. #1
    Join Date
    Jul 2004
    Posts
    92

    AccessControlException with applet

    ok. final project in the class. had to make an application that takes a zip code from user input and outputs it backwards using JFrame stuff. then, convert the application to an applet. JFrames on their own are really plain and ugly so i did some color and font stuff to try to dress it up. out text doesn't show how to use an image in an applet, but they way everything is described, and applet is the same as an application except the main method is gone etc.

    so i get my application to work just fine in the jGrasp program that our class uses to make it's programs (works great with the image . . . ). then i make the necessary changes and convert it to an applet. and it STILL works fine. looks good, infact. so i transfer all my files to a particular folder on my computer (all my class files for the applet and the jpg image). then i upload them all using dreamweaver to the remote site.

    the applet won't load when you visit the website, though. won't work at all. i loaded it several times before to see how it would look BEFORE putting in the image so i know that i am doing the uploading correctly. i asked my instructor and he told me to "stop worrying about it." that the "code looks fine". well, that's great and all, but not being able to use an applet that has an image is a big problem i would say. and since i am going on to the NEXT level of computer programming in a month (fall semester), i think i need to know what is going on.

    it is DEFINATELY something with the image. i even tried using ANOTHER image thinking that this one was corrupt. i tried making ANOTHER page. tried making ANOTHER applet that just had an image ONLY. same problem. everything works JUST GREAT until you try to look at the page on the web and you get a big grey square with a little red "X" in the left top corner.

    when you right click on the little red "X" it gives a whole bunch of lines, and i would copy and paste them all right here, but the window that comes up won't allow me to copy or paste or even Print!! but the first line says
    java.security.AccessControlException: access denied(java.io.FilePermission alien2j.jpg read)

    and then there are about 10 or 12 more lines. all referring to the image more or less.

    can someone give me a SOLUTION to this problem since my instructor doesn't seem to think it is important!!

    you can check out the site and the error message at
    www.unc.edu/~cerogers/ and then click on MarsZip below the first applet that is on the index page.

    any help would be great.

    thanks
    crq

  2. #2
    Join Date
    May 2004
    Posts
    219
    Yep, that's normal. If you told your instructor the exact exception, he probably would have explained it to you, unless he was just being lazy Anyway, applets are designed for the web. Because of this, there has to be a lot of security that comes with it. If you clicked on a web page, and it loaded an applet, you wouldn't want it to be able to start reading files off your harddrive without your permission now, would you? By default, applets are only allowed to read files off the server. I'll look it up the exact method for this in a minute...

    Edit:
    Code:
    Image smile = getImage(getDocumentBase(), "smile.gif");
    getImage and getDocumentBase are inherited from Applet.

  3. #3
    Join Date
    Jul 2004
    Posts
    92
    thanks, but WHERE is that code supposed to go in my applet?

    crq

  4. #4
    Join Date
    Jul 2004
    Posts
    92
    and what is getDocumentBase?

    and yes, i told my instructor the EXACT exception. he WAS being lazy.

    thanks
    crq

  5. #5
    Join Date
    May 2004
    Posts
    219
    Um, where ever you were retrieving the image before?

  6. #6
    Join Date
    Jul 2004
    Posts
    92
    so in my case, the image is called
    alien2j.jpg

    so the sentence would read

    Image alien2j.jpg = getImage(getDocumentBase(), "alien2j.jpg");

    and so i suppose that for every image in all applets, you should include that code?

    thanks
    crq

  7. #7
    Join Date
    May 2004
    Posts
    219
    http://java.sun.com/j2se/1.4.2/docs/...et/Applet.html
    Code:
     URL	getDocumentBase() 
              Gets the URL of the document in which this applet is embedded.
     Image	getImage(URL url) 
              Returns an Image object that can then be painted on the screen.
    Read the API. You will learn so much once you are comfortable with it.

  8. #8
    Join Date
    Jul 2004
    Posts
    92
    oh, and one more question . . .

    so is an image not considered to be "a file" by an applet? only .class files are?

    thanks
    crq

  9. #9
    Join Date
    May 2004
    Posts
    219
    Originally posted by crq
    Image alien2j.jpg = getImage(getDocumentBase(), "alien2j.jpg");
    Image is a class that models objects, that not surprisingly, hold image data. Just like a String or any other Object, you should name it accordingly. Image myAlienImage ... or whatever you want.

    As for where to put it... what exactly does the code look like that displays the image you have now?

  10. #10
    Join Date
    May 2004
    Posts
    219
    Originally posted by crq
    oh, and one more question . . .

    so is an image not considered to be "a file" by an applet? only .class files are?

    thanks
    crq
    No, file describes any file. Applets just can't read files off of YOUR harddrive. If you notice, your current code probably works in appletviewer, because you essentially are the server in that case.

  11. #11
    Join Date
    Jul 2004
    Posts
    92
    yeah, i know it just can't read files off my hard drive, i just never figured that uploading my class files AND my image file INTO the remote server wouldn't work.


    import javax.swing.*; // for GUI
    import java.awt.*; // for GUI
    import java.awt.event.*; // for ActionListener

    // class MarsZip
    public class MarsZipapplet extends JApplet
    {

    private JLabel imageLeftL;
    private JLabel imageRightL;

    public MarsZipapplet ()
    {

    // instantiate JLabels for images
    imageLeftL = new JLabel("", SwingConstants.LEFT);
    ImageIcon alien1 = new ImageIcon("alien2j.jpg");
    imageLeftL.setIcon(alien1);
    imageRightL = new JLabel("", SwingConstants.RIGHT);
    ImageIcon alien2 = new ImageIcon("alien2j.jpg");
    imageRightL.setIcon(alien2);

    and then i set the locations and sizes for them.

    i even tried taking an empty JFrame content pane and putting it on the web. it showed up on the web. but when i changed it and put the image in, it became the same grey box with just the red "x" in the top left corner.

    thanks
    crq

  12. #12
    Join Date
    May 2004
    Posts
    219
    Well, you sir are in luck. If you look at the docs...
    Code:
    ImageIcon(Image image) 
              Creates an ImageIcon from an image object.	
    ImageIcon(String filename) 
              Creates an ImageIcon from the specified file.
    As you can see, ImageIcon has overloaded constructors. I'm sure you can fix your code now with what I showed you earlier, and with this information.

  13. #13
    Join Date
    May 2004
    Posts
    219
    Originally posted by crq
    yeah, i know it just can't read files off my hard drive, i just never figured that uploading my class files AND my image file INTO the remote server wouldn't work.
    I assume when you construct an ImageIcon with a String, it uses typical file i/o. So basically, it went to look for it on your drive and the VM's security manager said "hey whoa there, no you don't" and blocked it. I can see why you were confused.

  14. #14
    Join Date
    Jul 2004
    Posts
    92
    thanks for all your information. however, you are really really talking over my head here. i CREATED a new image icon.

    ImageIcon alien1 = new ImageIcon("alien2j.jpg");

    what is this?

    ImageIcon(Image image)
    Creates an ImageIcon from an image object.
    ImageIcon(String filename)


    are you worried about "giving me the answer" or something? i guess you don't have to take my word for it, but the class is over and my applet was turned in "as is". the instructor said the "codes" were fine and told me not to "stop worrying" about it . . . which naturally kinda irked me. i do this crash course 5 week super intensified java class only to have my last project, an applet, not load and my instructor tell me not to worry about it, that my project is fine like it is.

    i read the link and i really means nothing to me. the only reason that i wrote MY codes like that is because the ONLY example in our textbook that even USES an image used that code. i really don't even know WHAT that code is doing. ofcourse, the code in the text is for an APPLICATION. but also, according to the text, an application can be converted to a working applet by removing the main method, taking away the size setting and making sure that your class extends JApplet. and obviously that isn't always the case. namely, if an image is involved that isn't the case at all.

    so i really am trying to understand what you are saying. and i have tried to read stuff on and off from the java site during this course, but it is like greek to me UNLESS i already understand it.

    best i can tell, i created a JLabel, created a new image icon (whatever that is), and placed it IN the JLabel.

    sorry. i am just frustrated by this. and i maybe i wouldn't care about it at all if i wasn't going to start another programming course in JAVA in a month. and since our course skimmed over everything, i just don't feel prepared.

    thanks
    crq

  15. #15
    Join Date
    May 2004
    Posts
    219
    Code:
    Image alienImage1 = getImage(getDocumentBase(), "alien1j.jpg");
    Image alienImage2 = getImage(getDocumentBase(), "alien2j.jpg");
    
    imageLeftL = new JLabel("", SwingConstants.LEFT);
    ImageIcon alien1 = new ImageIcon(alienImage1);
    imageLeftL.setIcon(alien1);   
    
    imageRightL = new JLabel("", SwingConstants.RIGHT);
    ImageIcon alien2 = new ImageIcon(alienImage2)
    imageRightL.setIcon(alien2);
    This is a constructor for ImageIcon:
    ImageIcon(Image image)
    Creates an ImageIcon from an image object.
    In the code at the top, I created an Image using getImage() and stored it in alienImage1. So, at this point alienImage1 is an instance of Image that is holding the picture information. When you construct an ImageIcon, it can take an Image as a parameter.
    Code:
    new ImageIcon(alienImage1)
    An instance of ImageIcon is directly created using the 'new' keyword. Since one of the ImageIcon constructors takes an Image, you can pass your previously created alienImage1 to it.
    Code:
    ImageIcon(Image image) 
              Creates an ImageIcon from an image object.
    Code:
    Image alienImage1 = getImage(getDocumentBase(), "alien1j.jpg");
    ImageIcon alien1 = new ImageIcon(alienImage1);
    Just making sure you see the connection here. Don't worry too much about the class. If I were you, I would just continue working and reading from stuff out of your book/off the web (and most importantly, writing code!), so you will be more prepared for the next class. I assume this next one will be a semester long, though? If that's the case, I'm sure it will be much less hectic for you than this class was, as long as you ask lots of questions. In my experience, TAs are usually more willing to help and often more available, but YMMV. As long as your next class isn't too advanced, you'll be fine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center