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

Started by
14 comments, last by role 19 years, 6 months ago
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
		{
			BufferedReader fin = new BufferedReader(new FileReader(dictionaryFN));
			String word = fin.readLine();
			while (word != null)
			{
				dictionary.insert(word);
				word = fin.readLine();
			}
			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.
Advertisement
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.
Whoa! No, you've got completely the wrong idea.

Applets CANNOT READ FILES.

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
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.
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!
8! bits.. wow..
Applets CAN read files.
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
-------------Golden T Game Engine - Java Game EngineGolden T Website | Golden T Forum
Quote:Original post by role
Applets 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.
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.
-------------Golden T Game Engine - Java Game EngineGolden T Website | Golden T Forum
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.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Quote:Original post by CaptainJester
As 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.

This topic is closed to new replies.

Advertisement