# [java] Applet runs in appletviewer but not in any browser

This topic is 4901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

java.security.AccessControlException: access denied (java.io.FilePermission words.txt read) There is the error in my Java Control Panel Console thingy when I try to load my applet in a browser. I've tried IE and FireFox. Are applets unable to read files located in the same directory as the applet? Or is there something simple I have to change?
		// build the dictionary tree
String dictionaryFN = "words.txt";
try
{
while (word != null)
{
dictionary.insert(word);
}
fin.close();
}
catch (FileNotFoundException e)
{
System.err.println("FileNotFoundException: " + e.getMessage());
return false;
}
catch (IOException e)
{
System.err.println("IOException: " + e.getMessage());
return false;
}


There is the code in question. Maybe I'm doing something wrong? Any help is appreciated.

##### Share on other sites
well it might be because you don't have the java plugin installed as it longer comes with Internet exporer or fire fox,but there error sounds like the applet is trying to read a
file that's not on the site that hosted it.if the word.txt is in the same folder you probobly shouldn't have the latter problem.try looking to see if you have the java plugin.

##### Share on other sites
Whoa! No, you've got completely the wrong idea.

This is deliberate, it's a security feature. Hence the security exception.

There are two things to look into:

1. Manual changes to security policy file. The user can edit a text file and grant your applet priviledges to read files on their hard disk.

2. Signing the applet. Signed applets can do slightly more stuff.

Normally, if you need to read from a local file, then you have designed your applet wrongly. Normally, you should NOT be writing an applet (should be using an application instead) or you just need to re-think your design.

@hazle: applets run on the local machine, not the server, so it has nothing to do with that. Applets *can* read from the server they came from, using HTTP calls (i.e. by specifying a URL), but that's using networking, and so is different from using a BufferedReader to a File.

redmilamber

##### Share on other sites
I see. I forgot that the browser actually downloads a copy of the applet to run on the client machine, so not being able to read files is probably a good thing. Thanks.

I guess I'll look into retrieving the file from the server through HTTP like you said.

##### Share on other sites
try {    URL file = new URL(getDocumentBase(),"yourfile.txt");    BufferedInputStream buffer=new BufferedInputStream(file.openStream());    DataInputStream in=new DataInputStream(buffer);    String line;    while ((line = in.readLine()) != null) {      //handle data    }    in.close();    buffer.close();  }catch(MalformedURLException mue) {      System.out.println("Bad URL: " + mue);  } catch(IOException ioe) {      System.out.println("IOException: " + ioe);  }

That's how to do it!

##### Share on other sites
Put everything in a jar (the classes and the text files).
And use this to read the file:
try {	InputStream stream = getClass().getResourceAsStream("words.txt");	InputStreamReader in = new InputStreamReader(stream);	BufferedReader readIn = new BufferedReader(in);	String word;	// reading text	while ((word = readIn.readLine()) != null) {		// dictionary.insert(word);	}	readIn.close();} catch (Exception e) {	e.printStackTrace();}

Note: in above code, the file must be in the same path as the class that load it.
If the class in com.blabla package then the file must be in com/blabla directory

##### Share on other sites
Quote:
 Original post by roleApplets CAN read files.

Well, technically speaking, of course they can if you grant permissions manually ;D.

Quote:
 Put everything in a jar (the classes and the text files).

That's not reading a file, it's just fetching something from the server using a URL connection.

OK, so the method calls look different, but that's all it's doing (with the benefit of transparent local caching if available).

Quote:
 Note: in above code, the file must be in the same path as the class that load it.If the class in com.blabla package then the file must be in com/blabla directory

Are you sure? I thought that wasn't the case although it is possible. Isn't the resource loader for a class normally just a delegator to the system/classloader resource loader - which has no concept of particular packages. Personally, I generally go for an explicit reference to the system resource loader to avoid any confusion over where you're loading from, so my memory is hazy of doing it using the local class's resource loader.

##### Share on other sites
Okay, one question, have you try it?
Three of my games is reading files in Applet based, and nothing to do with setting permissions.
And so far all the games is working perfectly.

##### Share on other sites
As was shown above, you CAN read files, but not from the local machine. They either have to be read from the URL that the applet came from or you have to jar everything up and read it as a resource. There are examples of both above.
Quote:
 Are you sure? I thought that wasn't the case although it is possible. Isn't the resource loader for a class normally just a delegator to the system/classloader resource loader - which has no concept of particular packages. Personally, I generally go for an explicit reference to the system resource loader to avoid any confusion over where you're loading from, so my memory is hazy of doing it using the local class's resource loader.

The classloader will put you in the directory in which it located the class file. You can use "/" to move yourself to the root of the resource tree.

##### Share on other sites
Quote:
 Original post by CaptainJesterAs was shown above, you CAN read files, but not from the local machine.

Different definitions: I define "read files" as "read a file from a filing system". Sure, it's "all just streaming bytes", but downloading something using HTTP doesn't count as a file-system (unless it is actually a virtual locally-mapped FS, in which case...you can't do it in an applet!) as far as I'm concerned.

OS's consider network connections and FS's fundamdentally different, and the failure modes, behaviour, and low-level implementations are fundamentally different. Most apps need to be aware of the differences.

Quote:
 The classloader will put you in the directory in which it located the class file. You can use "/" to move yourself to the root of the resource tree.

Ah, OK. Thanks.

##### Share on other sites
All right, I tried role's method of putting everything in a jar file and reading the file as in InputStream, but I still get the same security violation.

this line is the culprit:
	InputStream stream = getClass().getResourceAsStream("words.txt");

words.txt is definitely included in the jar file and its path is the same as the class that loads it.

##### Share on other sites
Some quick example:
http://goldenstudios.uni.cc/products/games/test.zip

and run the applet page: test.html

java source code: TestCase.java
packing: jaring.bat

If above sample still throw security permission exception, please test this game:
Roboticswar
The game read file just like my sample above, and it is tested by many ppl in applet mode and so far no one ever experienced with the security permission.
If the game still not working, please tell me what JDK you use right now.

##### Share on other sites
Thanks, your code doesn't work either. Although your Roboticswar game does (very nice btw). I'll keep screwing around with it, maybe i'll just make it a swing application. I have Java 1.4.1_02.

##### Share on other sites
Veryyyy weirddd....
My games and the sample do read the file with same technique.
How can the game can be run but the sample not???
I also have tried the sample on cyber cafe with Opera browser and the reading works nicely.

Quote:
 maybe i'll just make it a swing application

You mean you want to make it application instead of applet? the 'swing' thing makes me confuse, cos in swing there's JApplet too.

##### Share on other sites
Perhaps the poster has (deliberately or accidentally) altered their local security policy file?

##### Share on other sites
AP is the applet runs on your pc?