Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-10-26
14
Medium Priority
?
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 

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 1200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

597 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