java.security.AccessControlException
Ok, so when i tried to read a file in using an applet, i got this error. java,security.AccessControlException: access denied<java.io.FilePermissions C:\\a.txt read> I called the method that reads the file from the init method and as soon as i tried to create a file reader or see if the file was readable i got this error. Here is my code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class fileReader extends Applet
{
static Random random = new Random();
public void init()
{
fileContents();
}
public void paint(Graphics g)
{
}
public static void fileContents()
{
File file = new File("C:\\h.txt");
System.out.println(file.canRead());
System.out.println(file.exists());
System.out.println(file.getAbsolutePath());
try
{
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
}
catch (Exception e)
{
System.out.println("There was an error");
}
}
}
So, what am i doing that is causeing this error and how can i fix it?
You have hit what I call the wall of death. But keep in mind I am a novice java programmer. Your Windows Operating System has certian securities that cannot be removed which will not let applets directly access your hard drive.
1) you need to learn JDBC
2) you need to semi learn a data base system
3) then you will need to create a data base and get the appropriate java adapter
4) you will use that adapter in your JDBC to access your Data Base
All in all this is why I call it the WALL OF DEATH. I got so discouraged by this that i quite programming. But then I picked it back up to make applications for my classroom. I still need to take classes to learn all this. Go to your local community college and take classes there.
THERE IS NO QUICK SOLUTION FOR THIS PROBLEM
SORRY ((you can cry.... I did)))
The earlier reply is not correct. Applets are prevented from doing certain things - reading files is one - to maximize security. You can either add permissions to the computer that the applet is running on, or you can sign the applet.
See
http://java.sun.com/docs/books/tutorial/security1.2/tour1/step2.html
and
http://forum.java.sun.com/thread.jspa?threadID=686184&tstart=45
if you find an easy way please send an email to me. But I looked for a long time for an easy solution.
The only hint of a solution was told to me by a professor who is an employee at Sun Micro and a professor at my local community college. He said "Why are you going to learn data base and JDBC when you can just use a servlet" .
He did not have time to elaborate on his statement. I asked around on different forums but never got a response. So i cried and cried and played EVERYQUEST. 2 years later I emerged much skinnier and very pale. <laugh>
You may want to refer to
http://java.sun.com/docs/books/tutorial/security1.2/index.html
in particular:
http://java.sun.com/docs/books/tutorial/security1.2/tour2/step3.html
BTW, you can "solve" access control exception by changing *your* policy file. But, when you deploy your applet in a webserver (assuming you'll do), then it won't work -- because your user's policy will most probably won't permit reading local files. In general, most applets are designed to work in strict "sandbox" -- where not many permissions are given.