Solved

I'm able to load images using applet viewer but with html I get access denied exception

Posted on 2003-10-26
14
331 Views
Last Modified: 2013-12-29
java.security.AccessControlException: access denied (java.io.FilePermission) error occurs when I execute applet using html.
It actually works for me if the path of images is under myapplet directory - where my applet classes exist but when I move my images directory somewhere else on the c: drive and then point to it in html it gives me io exception mentioned above.

I need to do this since my client asked me to make the path of images configurable - passed as a parameter to applet.
For example, image directory will be passed as c:\test\images.

In applet viewer it always works under all circumstances. In html it only works if images directory is under my applet classes.

Thanks in advance
0
Comment
Question by:healingtao
  • 6
  • 6
  • 2
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9623442
Use one of the applet parameters to configure the path, but make it relative to the code base. Get it like so:

String path = getParameter("imagePath");
URL url = new URL(getCodeBase(), path);
Image image = getImage(url);

You should be OK then
0
 

Author Comment

by:healingtao
ID: 9623773
CEHJ,

Can you please provide an example. For instance what should getCodeBase() return?
Right now in my html for codebase I have
<applet codebase="."

right now I do the following
btLeft = new JButton(new ImageIcon(path + (image_name)); to get image.
please show an example in this context.

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9623795
getCodeBase() returns the directory in which the applet lies. The codebase attribute in the tag that you mentioned simply allows an 'offset' between the applet codebase and the html file.

Just combine your code with the code I already posted:

String path = getParameter("imagePath");
URL url = new URL(getCodeBase(), path);
Image image = getImage(url);btLeft = new JButton(new ImageIcon(image));

In the applet tag there should be a parameter called 'imagePath' with a value like 'images/someImage.gif'
0
 

Author Comment

by:healingtao
ID: 9623832
quick question before I do this. In my applet I have many images.
My concern is twofold.
a) performance - it seems that I have to do this for every image since you specified that
>>'imagePath' with a value like 'images/someImage.gif'. Does this mean that I have to call url
URL url = new URL(getCodeBase(), path); for every image since it appears that path here already includes image
Is it going to be slow? because performance is very important for this project
b) How to set this up generically since I might have up to 100 images to load.
0
 

Author Comment

by:healingtao
ID: 9623835
in my parameter I just have the path and not the image names since everywhere I do the following
btLeft = new JButton(new ImageIcon(path + (image_name)); to get image.
0
 

Author Comment

by:healingtao
ID: 9623894
I did what you mentioned for one of my images the following way
btLeft = new JButton(new ImageIcon(getImage(new URL(getCodeBase(), path + image_name))));

I then got malformedURLException
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9623908
You've got to be able to specify the images somehow haven't you? You could have a text file specifying the images and load them based on its contents.

>>I then got malformedURLException

Print it first and post it here
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:healingtao
ID: 9623938
I got java.net.MalformedURLException: unknown protocol: c
then I checked  String val = getCodeBase().getPath(); and I got the following path
/C:/DOCUME~1/Owner/LOCALS~1/Temp/
This is definitely not where my class is located
0
 

Author Comment

by:healingtao
ID: 9623942
>>  You've got to be able to specify the images somehow haven't you?

Well, I understand that, but as I mentioned before, I was just concerned with performance since it seems
that I have to do new URL for every image and I have many of them.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9623947
The path parameter should be relative and contain no drive letter
0
 

Expert Comment

by:chillyspoon
ID: 9626122
I think this thread has drifted away from the original needs of healingtao - you simply wish to display a large number of images in your applet.

The security errors you were originally getting were due to the security restrictions applied to applets when they are run from within web browsers to avoid mailicious use - what you could consider doing is "signing" your applet with a certificate, this will give your applet the same level of rights (or whatever level of rights you specify) when run within the web browser as in appletviewer.

Have a look at this article on the Sun Microsystems web site, it's one of the most useful I have ever read concerning differences between applets and java applications regarding the restrictions upon applets:
http://developer.java.sun.com/developer/technicalArticles/Security/Signed/

Applying a certificate will definitely allowed you to use local paths to your images (for example in an signed applet I have just written I use JFileChooser to select images), you do not have to use relative paths or use getCodebase(), which will restrict where you can store the images.

All the best,

chillyspoon.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 9626138
>>I think this thread has drifted away from the original needs of healingtao

On the contrary - it's beginning to drift away now as you introduce the new element of signing the code - an operation which is expensive, tricky, alarming to users and is to be avoided if possible.
0
 

Expert Comment

by:chillyspoon
ID: 9648927
I do not consider offering an alternative to be drifting away from the topic, furthermore your comments on applet signing are largely inaccurate.

Firstly signing an applet is not expensive, in fact it's 100% free - a cost is only necessary if you require that signing to be approved by another body for the purposes of serving members of public. The chances are, that when producing an applet as a front end for a J2EE (or otherwise) application, that the signing will not need approval by another body as it will be used by staff of the company who are signing the applet in the first place - hence it remains free.

Secondly - a tricky process, again I would point to the included article, as I thought it was a tricky process until I followed the mentioned steps and saw it working first go..

Thirdly - alarming, ok I definitely do agree with you on this one, if browsing a web site and a certificate request pops up, the majority of people will panic. However, this only applies to a member of the public, if you are producing software for another company, staff will know in advance what to do the first time they encounter a certificate - it's simply a part of training.

Fourthly - to be avoided if possible, I just say it's a case of horses for courses, you either need the level of access signing provides or you don't.

In healingtao's case it looks like it wasn't necessary, but that doesn't make a suggestion of something that may be of use, or a technology any less valid.

Regards.



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9652107
>>
Firstly signing an applet is not expensive, in fact it's 100% free - a cost is only necessary if you require that signing to be approved by another body for the purposes of serving members of public
>>

Well, i assume, especially since healingtao uses the word 'client' that he is producing professional software, so I don't see how that approval could be avoided.

>>
The chances are, that when producing an applet as a front end for a J2EE (or otherwise) application, that the signing will not need approval by another body as it will be used by staff of the company who are signing the applet in the first place - hence it remains free.
>>

Unless you know more about healingtao's business that you are revealing, the above seems to be a rather strange assumption or speculation
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java virtual machine monitoring tools 3 39
copyEndy  challenge 15 58
maven java path setting 5 50
Running Jira on Raspberry PI 2? 3 137
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now