Newbie problem - My applet fails in browser

R

Rob Jaeger

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
 
L

Larry

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.
 
R

Rob Jaeger

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)
 
R

Roedy Green

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
 
A

AJ

Rob Jaeger said:
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.
 
R

Rob Jaeger

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
 
R

Roedy Green

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.
 
L

Larry

Rob Jaeger said:
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().
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top