Java Programming question + Security..

Discussion in 'Archived Threads 2001-2004' started by Jay H, Apr 5, 2002.

  1. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
    Hi guys and gals,

    Learning Java programming now, been playing with a small applet I've created, playing around with the Layouts and making buttons and handling events and stuff. I have a small applet that takes a parameter from the tag in HTML and it accepts either a local JPG or a URL of a picture and displays in in a Panel using the AWT Graphics API.

    Now, when I use Appletviewer which comes with the 1.3 SDK, it works fine, loads up a locally stores JPG on my hard drive, but whenever I use Netscape to try it, I get an accessviolation, whenever it tries to load the JPG (either locally or if I input a URL in the APPLET tag using PARAM).

    Is that a security feature? Do I need to somehow read it in as "read-only" or something?

    Jay
     
  2. Kevin P

    Kevin P Screenwriter

    Joined:
    Jan 18, 1999
    Messages:
    1,439
    Likes Received:
    0
    Java applets on a web site can't access files on your hard drive. This is a security feature. However, if you put the jpgs you're trying to display on the web site as well, it should be able to display them from there.

    KJP
     
  3. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
    Yeah, I know the hard drive stuff but I put all the *.class files and the test JPG on my website, still had an accessviolation... perhaps I'll check the settings of that file.

    Seems like you should be able to pull in any image on any website, at least you're given the option to create new object of class "URL" and then use that to create an image of class "Image" and use the Toolkit to open it.

    I shall continue to tweak!

    Jay

    Here's my error from the Java console window:

     
  4. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
    Do *.class files and images used by the class files have to have certain permissions set? i.e. the rwx bits? I don't have a shell access on my ISP but I supposedly do CHMOD via FTP except when I try, it says it's not allowed from the remote server. When I get a DIRINFO, it shows that all my files are set to 777 which would be RWX for all three groups, is that a problem???

    I've got another problem with my graphics not showing up til I do a 'restart' applet but I know that's a coding issue which I am trying to resolve. but when my applet is initialized, I get that accessviolation exception, whereas in appletviewer on my PC, it works fine.

    Jay
     
  5. MichaelG

    MichaelG Second Unit

    Joined:
    Jul 10, 2000
    Messages:
    322
    Likes Received:
    0
    Jay,
    The appletviewer doesn't use the same SecurityManager that the Web browser uses, for the most part you can do what you want from an appletviewer. In the browser the SecurityManager will not allow you to access ANY files on your local machine ~unless~ the Applet is signed.
    There is a simple example of how to load images with an Applet using the MediaTracker class. Look in the Java Docs for java.awt.MediaTracker, on the 1.3.1 Docs they have sample code for an Applet.
    Keep in mind that the images must be in a directory structure under the Http Server's root directory. So, if the root directory of the Http Server is
    C:Apachehtml_root
    and you are accessing image files (as in the Java Doc example) like this...
    getImage(getDocumentBase(), "images/anim1.gif");
    make sure that the images directory is here...
    C:Apachehtml_rootimages
    I realize that you may already know this, but I just wanted to throw that out
    Mike
     
  6. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
    aahhhhh, damm, I have to have access to the Apache Server? I thought I could just load it to my ISP's free webspace and then run it from there from ~username/public_html..

    Oh well, I've really busy but I'll have to check out that MediaTracker, I've heard about that class with buffered images but haven't looked at it yet. I'm having another problem with my images is that my overloaded PaintComponent is not painting the component right away, but after I either "restart" the applet or force the OS to refresh the screen, i.e. drag a modal dialog box over it... the problem is I haven't found a way to call PaintComponent explicitely with a object of class "Graphics" I've yet to figure out how to Use GetGraphics properly (other than looking at other code snippets) so when I have the time I'll have to post some code snippets.

    Basically I have an init() routine from a class called at after the Panel object is created. it reads a file from Toolkit into an object of class Image. Now I have an Image object, but I'm confused how to get that to draw when my applet is started. I overloaded Paintcomponent(Graphics g) like I see in examples and that works, but at initialization, I can't figure out how to explicitely call PaintComponent to get it to draw at startup...

    More to come..

    Jay
     
  7. Kevin P

    Kevin P Screenwriter

    Joined:
    Jan 18, 1999
    Messages:
    1,439
    Likes Received:
    0
     
  8. MichaelG

    MichaelG Second Unit

    Joined:
    Jul 10, 2000
    Messages:
    322
    Likes Received:
    0
    Jay, sorry about the confusion, Kevin is right. I just thought that you had access to the Http Server, didn't realize that it is a remote Server that you don't have access to. The getDocumentBase() call "Returns an absolute URL naming the directory of the document in which the applet is embedded", as stated in the Docs. Example...
    Applet is embeded in the following HTML file
    http://www.yourserver.com/something/index.html
    The document base is:
    http://www.yourserver.com/something/
    So, if yuo create an images directory under the something directory then the call to
    getImage(getDocumentBase(), "images/anim1.gif");
    would return the image file located at
    "http://www.yourserver.com/something/images/amin1.gif"
    Also, the reason the the PaintComponent may not be painting correctly right away could be because the image hasn't finished loading yet. That's what you can use MediaTracker for. With MediaTracker you can tell it to wait until a specific image (or other media) or all images (or media) have loaded before continuing. So if you put that code in your init method that gets called by the contructor then the images will load before the Applet is painted.
    Hope this helps
    Mike
     
  9. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
     
  10. MichaelG

    MichaelG Second Unit

    Joined:
    Jul 10, 2000
    Messages:
    322
    Likes Received:
    0
    The reason you need to use getImage(getDocumentBase()+...) instead of the Tooklit.getImage() is because you are using an Applet. If your java application wasn't an Applet, but a stand alone application you would use the Tooklit.getImage() method. Toolkit.getImage() accesses the local drive directly, but since and Applet doesn't have the Security to do this you must access it through HTTP (which is how the Applet.getImage() method works).

    Due to the Security Restrictions with an Applet you can't just read information from a local harddrive ~unless~ the Applet is signed. When you sign and Applet the user is asked if that Applet is safe to download from the source. So, you need to make sure that the source is valid. Just like you might see a signed Applet from Microsoft Corp that installs some software. A signed Applet has access to the local drives, at which point you could read/create/delete files.

    Does that answer your question about preferred method to load an image?
     
  11. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
    Yup MichaelG.. I actually found that out when I tried to change from the GetToolkit to the getImage method... The GetDocumentBase is not a method of class JPanel which is what my ImagePanel inherits so I got an error when I compiled.. Looking at the 1.3 docs, I saw that it's a method in class Applet so I had to move that stuff into my main Applet class and get the image there and everything works.
    Now it works fine for locally stored images but I'm trying to get it to work for Image URL's like
    http://yoursite.com/picture.jpg
    but I am getting another accessviolation. Need to check my code again, and add some debugging stuff for it. But I also got MediaTracker to work and now that's working.. yippee!
    Jay
     
  12. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
    Well, everythings working well, gonna go tweak some more, but I notice now that it will not work in Netscape 4.7 without the Java 2 plugin, where before (I don't remember at what stage) it did. I guess one of the classes I added recently is not in the old JVM included with NS 4.7. Is there a way to find out which classes are included with which JVM version?

    Could it possibly be MediaTracker or URL?

    Those are the two most recent class objects I've added...

    Jay
     
  13. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
    Is it another security reason why I can't seem to open a file on my webserver, even though I can open a file at initialization? I have a small image viewer applet that uses a pulldown list and a panel that displays it. The user will be able to select a picture from the list and that picture will be displayed in the box. I have a bunch of .jpgs in my user directory and the *.class files but for some reason, the applet can load the initial file, except I get that "access denied" whenever I try to select a different picture.

    Is there a reason why the applet can load an image at startup but cannot load an image while running? I would think the security hazard would be there in either case!

    I have an actionListener event that handles the pull down menu, whenever the user selects a file, the program tries to retrieve the file into an Image and then redraw the picture on the screen. Gets Access Denied whenever selecting a new picture.

    Jay
     
  14. Bill Slack

    Bill Slack Supporting Actor

    Joined:
    Mar 16, 1999
    Messages:
    837
    Likes Received:
    0
    If you open the file for RW and never close it, the file is most likely locked, so another handle cannot be associated with it.
     
  15. Jay H

    Jay H Producer

    Joined:
    Mar 22, 1999
    Messages:
    5,654
    Likes Received:
    32
    Location:
    Pittsfield, MA
    Real Name:
    Jay
    I spent some time this morning putting in some debugging code... I found the problem, it was the URL and not the method that was bad... So now it works... Unfortunately, the exception that a "file not found" throws seems to be "Access Denied" or something which led me to believe that the problem was opening/reading a file, when in fact there was a problem with the URL that I was giving it... Oh well, live and learn.
    [​IMG]
    Oh, while I'm here, is there a function that returns a String containing the domain that the applet is on? It seems that "getDocumentBase()" returns everything but the domain part of the URL. I don't think I need it per se but I already am appending the string to my URL that I pass to getImage so I was just curious...
    Jay
     

Share This Page