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?

[1277 byte] By [h3lladvocatea] at [2007-10-2 12:59:24]
# 1

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)))

bryanbennetta at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Well, isnt that just lovely. There must be a simpler way... Im to busy to leatn JDBC (whatever that is) and data base.
h3lladvocatea at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

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

ChuckBinga at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

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>

bryanbennetta at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
Well, im using the appletveiwer that comes with JCreater so shouldnt it allready be signed?
h3lladvocatea at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6
I don't know.. I use TextPad and Command Line
bryanbennetta at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 7
Ok, i open up policytool and it said to add a new policy. Now how do i tell it to allow appletviewer.exe to access files and read/write them?
h3lladvocatea at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 8

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.

sundararajan.aa at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 9
Ok so i went to the java.policy file in the jre and edited that to allow all permissions but it still dosnt work. It might be because i messed with the java.sequrity file too much so should i re-download the jre?
h3lladvocatea at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 10

Ok, i think my computer is retarded. No matter what i do to the java.security file, it does the same thing. In the tutorial it told me to add policy.url.3=file:/C:/Test/mypolicy to it under the policy.url.2 but despite that, it still dosnt load it. I even modified the java.policy file which is the main file it loads and i set it to allPermissions and it still dosnt work. What am i doing wrong? I am using notepad to edit the java.security file. 5 Dukes to whoever answers it correctly.

h3lladvocatea at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 11
Ok, i now know the problem, the only problem is how to fix it. It appears even though i edit the java.security file, it dosnt reconize it because i set the J-Djava part to false and it still let me use it. So how do i tell it to read the java.security file?
h3lladvocatea at 2007-7-13 10:18:25 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...