Jump to content
  • Advertisement
Sign in to follow this  
Malone1234

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

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

If you intended to correct an error in the post then please contact us.

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
		{
			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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!