Newbie problem - My applet fails in browser

Discussion in 'Java' started by Rob Jaeger, Sep 8, 2003.

  1. Rob Jaeger

    Rob Jaeger Guest

    I'm an experienced programmer working on my first Java applet in Eclipse.
    All is well if I don't try to access any external files, but if I try to
    even read a .jpg file, my applet fails (in browser). When testing within
    the Eclipse environment, all is fine. My browser has the most current
    version of Java.(2.1.4).

    To no avail, I've tried:
    making a JAR file, and including it in the html
    editting java.policy.applet with explicit FilePermissions

    TIA,

    Rob
    Rob Jaeger, Sep 8, 2003
    #1
    1. Advertising

  2. Rob Jaeger

    Larry Guest

    Can you post the code that is trying to access the files?
    And post and exception that is being generated?

    Generally speaking, within an applet, you can only access files using URLs,
    to access the directly as files, you will need to sign your applet and have
    the user grant you the appropriate permissions.

    "Rob Jaeger" <> wrote in message
    news:Xns93F06D8B4A447yogirob2yahoocom@24.168.128.90...
    > I'm an experienced programmer working on my first Java applet in Eclipse.
    > All is well if I don't try to access any external files, but if I try to
    > even read a .jpg file, my applet fails (in browser). When testing within
    > the Eclipse environment, all is fine. My browser has the most current
    > version of Java.(2.1.4).
    >
    > To no avail, I've tried:
    > making a JAR file, and including it in the html
    > editting java.policy.applet with explicit FilePermissions
    >
    > TIA,
    >
    > Rob
    Larry, Sep 8, 2003
    #2
    1. Advertising

  3. Rob Jaeger

    Rob Jaeger Guest

    "Larry" <> wrote in
    news::

    > Can you post the code that is trying to access the files?
    > And post and exception that is being generated?
    >
    > Generally speaking, within an applet, you can only access files using
    > URLs, to access the directly as files, you will need to sign your
    > applet and have the user grant you the appropriate permissions.
    >


    OK- I don't want to have a signed applet. I just need to read graphics
    files. I was hoping that just using the filename was good enough.

    The offending code:

    // board
    static Image imageBoard =
    Toolkit.getDefaultToolkit().getImage("board.jpg"); /* fails in
    browser, OK in Eclipse */

    It doesn't work at all if I specify the full URL (syntax error???):

    static Image imageBoard =
    Toolkit.getDefaultToolkit().getImage
    ("http://www.normaldistribution.com/PokerCalc/board.jpg");


    error (from Java Console):

    java.security.AccessControlException: access denied (java.io.FilePermission
    board.jpg read)


    Full Java Console output:

    java.lang.ExceptionInInitializerError

    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.applet.AppletPanel.createApplet(Unknown Source)

    at sun.plugin.AppletViewer.createApplet(Unknown Source)

    at sun.applet.AppletPanel.runLoader(Unknown Source)

    at sun.applet.AppletPanel.run(Unknown Source)

    at java.lang.Thread.run(Unknown Source)

    Caused by: java.security.AccessControlException: access denied
    (java.io.FilePermission board.jpg 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 sun.awt.SunToolkit.getImageFromHash(Unknown Source)

    at sun.awt.SunToolkit.getImage(Unknown Source)

    at aFont.<clinit>(aFont.java:49)

    ... 11 more

    java.lang.NoClassDefFoundError

    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.applet.AppletPanel.createApplet(Unknown Source)

    at sun.plugin.AppletViewer.createApplet(Unknown Source)

    at sun.applet.AppletPanel.runLoader(Unknown Source)

    at sun.applet.AppletPanel.run(Unknown Source)

    at java.lang.Thread.run(Unknown Source)
    Rob Jaeger, Sep 8, 2003
    #3
  4. Rob Jaeger

    Roedy Green Guest

    On Mon, 08 Sep 2003 16:58:10 GMT, Rob Jaeger <>
    wrote or quoted :

    >OK- I don't want to have a signed applet. I just need to read graphics
    >files. I was hoping that just using the filename was good enough.


    However, if the system let you read that file, you could go looking at
    the user's porn collection just as easily. That would be quite and
    embarrassing thing for an enemy to be able to do.

    That is why there are signed Applets and why your Applet can't go read
    an arbitrary file without permission.

    It is not that hard. See
    http://mindprod.com/jgloss/signedapplets.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 8, 2003
    #4
  5. Rob Jaeger

    AJ Guest

    Rob Jaeger <> wrote in message news:<Xns93F06D8B4A447yogirob2yahoocom@24.168.128.90>...
    > I'm an experienced programmer working on my first Java applet in Eclipse.
    > All is well if I don't try to access any external files, but if I try to
    > even read a .jpg file, my applet fails (in browser). When testing within
    > the Eclipse environment, all is fine. My browser has the most current
    > version of Java.(2.1.4).
    >
    > To no avail, I've tried:
    > making a JAR file, and including it in the html
    > editting java.policy.applet with explicit FilePermissions
    >
    > TIA,
    >
    > Rob



    Can we see some code? More specifically, any code involving the
    instantiation of the Image object, and all references to it? Also,
    what environment are you working in? *nix? What are your permissions
    for the JPEG? should be readable by everyone.
    AJ, Sep 8, 2003
    #5
  6. Rob Jaeger

    Rob Jaeger Guest

    Roedy Green <> wrote in
    news::

    > On Mon, 08 Sep 2003 16:58:10 GMT, Rob Jaeger <>
    > wrote or quoted :
    >
    >>OK- I don't want to have a signed applet. I just need to read graphics
    >>files. I was hoping that just using the filename was good enough.

    >
    > However, if the system let you read that file, you could go looking at
    > the user's porn collection just as easily. That would be quite and
    > embarrassing thing for an enemy to be able to do.


    Thanks, Roedy. I understand the security situation. For this applet, I
    just need to load some graphics files that reside on the server with the
    ..html and the .class. The graphics load fine from html, and the file
    permissions are fine.

    When I use the full URL like this:

    static Image imageBoard =
    Toolkit.getDefaultToolkit().getImage
    ("http://www.normaldistribution.com/PokerCalc/board.jpg");

    it fails even in the Eclipse environment. Is there something wrong with
    my syntax? You can see that the URL works.

    Thanks for your help & patience,

    Rob
    Rob Jaeger, Sep 8, 2003
    #6
  7. Rob Jaeger

    Roedy Green Guest

    On Mon, 08 Sep 2003 20:45:56 GMT, Rob Jaeger <>
    wrote or quoted :

    >Thanks, Roedy. I understand the security situation. For this applet, I
    >just need to load some graphics files that reside on the server with the
    >.html and the .class. The graphics load fine from html, and the file
    >permissions are fine.


    the easiest way to do that is to put them in the jar and get them with
    getResource.

    The usual way you trip over security is trying to pick them up from a
    different server from the one that loaded the Applet.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 8, 2003
    #7
  8. Rob Jaeger

    Larry Guest

    "Rob Jaeger" <> wrote in message
    news:Xns93F0846F58358yogirob2yahoocom@24.168.128.74...
    > "Larry" <> wrote in
    > news::
    >
    > > Can you post the code that is trying to access the files?
    > > And post and exception that is being generated?
    > >
    > > Generally speaking, within an applet, you can only access files using
    > > URLs, to access the directly as files, you will need to sign your
    > > applet and have the user grant you the appropriate permissions.
    > >

    >
    > OK- I don't want to have a signed applet. I just need to read graphics
    > files. I was hoping that just using the filename was good enough.
    >
    > The offending code:
    >
    > // board
    > static Image imageBoard =
    > Toolkit.getDefaultToolkit().getImage("board.jpg"); /* fails in
    > browser, OK in Eclipse */


    Here is your problem. You need to use the URL class to specify the URL,
    passing the file name into the URL constructor, and passing the URL object
    into getImage().
    Larry, Sep 9, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders
    Replies:
    1
    Views:
    669
    John Saunders
    Nov 18, 2003
  2. Andrew Thompson
    Replies:
    0
    Views:
    412
    Andrew Thompson
    Aug 5, 2003
  3. Rune Andresen
    Replies:
    2
    Views:
    1,935
    Phil Powell
    Sep 17, 2003
  4. Replies:
    2
    Views:
    291
    Grant Wagner
    Dec 23, 2004
  5. dayo
    Replies:
    11
    Views:
    342
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page