aspose file tools*
The moose likes Applets and the fly likes Accessing External Resources from Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Accessing External Resources from Applet" Watch "Accessing External Resources from Applet" New topic
Author

Accessing External Resources from Applet

Sam Kaydee
Greenhorn

Joined: Mar 16, 2014
Posts: 11

Hi,

I am quite new to developing Applets in java. I recently made an applet that will play a particular sound whenever a button is pressed. The sound files are placed in the same directory as the applet class file and i access them by using .

The Applet runs fine in the appletviewer but whenever I try to open it using any browser like Chrome,IE,Firefox the applet loads but sound isn't played. I checked the java console and it showed "java.security.AccessControlException: access denied ".
My questions are:
1. How do I allow the applet to get access to those files?
2. When the Applet is running from the local file system why CAN NOT I access those resources via getCodeBase() ???
3. I googled and found that I can sign a JAR and that can have full access to the computer. But how do I create a JAR from an applet (which has no Main method) while it asks me to point to the main class in the manifest?

Thanks in advance.

P.S: I also found this Oracles's Java Security


Let there be Code!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Sam Kaydee wrote:1. How do I allow the applet to get access to those files?


Your code is asking the applet viewer to get those files from the server. So you have to configure the server to send them when it's asked. Just as if your HTML page had a link to the file, in other words.

2. When the Applet is running from the local file system why CAN NOT I access those resources via getCodeBase() ?


Because getCodeBase() is expecting to get them via HTTP from your server. Don't expect it to do special things when the applet is running locally, because applets aren't designed to run locally so there's no point in handling that specially.

3. I googled and found that I can sign a JAR and that can have full access to the computer. But how do I create a JAR from an applet (which has no class) while it asks me to point to the main class in the manifest?


I don't understand what you mean by that. An applet IS a class. Look at the beginning of your code -- doesn't it say "public class Something" there? As for the manifest, you don't need a Main-Class line entry for applets.

P.S: I also found this Oracles's Java Security


You could read that as saying "Don't waste your time writing an applet unless you are committed to dealing with this sort of thing on a regular basis". Is there a particular reason you're writing an applet? I ask because it seems like you don't quite understand where an applet might be used, and you could probably spend your time better doing something else.
Sam Kaydee
Greenhorn

Joined: Mar 16, 2014
Posts: 11

I don't understand what you mean by that. An applet IS a class. Look at the beginning of your code -- doesn't it say "public class Something" there? As for the manifest, you don't need a Main-Class line entry for applets.


I am sorry that was a typo,what I meant was that an applet does not have a main method.The export options of eclipse requires me to specify a main class. However in a java applet there is no main class!
Is there a particular reason you're writing an applet?


Actually it was a Swing based application and everything was fine with but I had to convert it to an Applet for a friend and problems came pouring in

The applet runs perfectly in the appletviewer provided by JDK. So,basically I should have a HTTP server running to test if my applet isn't having any problem in the browser.is that right?

And is there any way that I can allow File System Access to the applet?( like Signing it?)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, applets are designed to be run from an HTTP server, so you shouldn't try to use the applet viewer to do anything other than that. Don't let your applet access files locally if those files aren't going to be on the client computer in real life. Presumably your friend expects to run it from an HTTP server, and do all of the signing necessary for a modern applet. (And hopefully your friend is competent to do all of that and isn't going to try to dump the security issues on you too?)

As for producing a jar from Eclipse, if it's asking you to specify the main class then you're doing something wrong, I think. Maybe you have to tell it that your project is for an applet when you create the project?
 
jQuery in Action, 2nd edition
 
subject: Accessing External Resources from Applet