1

Okay so I was parsering an XML file using JAVA DOM. Whenever the program gets to the point where it is supposed to parser the XML file, I get a "AccessControlException" saying "access denied". I have parsered many XML files using JAVA DOM and this is the first time I have gotten this exception. What am I doing wrong?

Here is the XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<root>

</root>

Here is the code that is supposed to parser it:

DocumentBuilderFactory bdf = DocumentBuilderFactory.newInstance();
        DocumentBuilder bd = bdf.newDocumentBuilder();
        Document doc = bd.parse("excersize.xml");

Finally here is the error I get:

java.security.AccessControlException: access denied ("java.io.FilePermiss
ion" "\excersize.xml" "read")

Edit

Luckily after some time I got it to work using a policy file however for whatever reason it only works if I type the full directory of the xml file

This works:

Document doc = builder.parse("file:/B:/Programming/Java/Programs/new/excersize.xml");

but this doesn't:

Document doc = builder.parse("excersize.xml"); //The xml file is in the same directory as the java source file, the class file, and the html file

although this is tolerable, I do like to keep my files "mobile ready" so that they don't have any concrete adresses but rather adresses relative to the .java and .class file. Any help at all to help me figure out this problem would be greatly appreciated :)

Nigh7Sh4de
  • 395
  • 2
  • 7
  • 20

1 Answers1

2

The exception says it all, you are trying to access a file the JVM can't access. Maybe you can take a look at the documentation

EDIT

By default, applets can't access the client's file I/O. You need to sign your applet or edit the policy files to allow it

Davz
  • 1,530
  • 11
  • 24
  • yes thank you I can read, but how do I give it permission so the error doesn't come up? – Nigh7Sh4de Aug 23 '12 at 18:03
  • I made a java.policy file with the proper permissions and put it in the same directory but it still isn't working. Do I have to declare my policy file in the java file? and if I do then how? – Nigh7Sh4de Aug 23 '12 at 21:16
  • never mind I got it now... I had to add a line to my jre/lib/security/java.security file, the line was: policy.url.3=file:/B:/Programming/Java/java.policy (where java.policy was a policy file I made myself). Only REALLY annoying thing is that the policy only applies if I type out the full location of the xml file (even though it's in the same directory) I have no idea why this is and any help would be appreciated. – Nigh7Sh4de Aug 23 '12 at 22:14
  • @Guitarroka Can you please show the content of your custom policy file? If for example XMLs you are parsing are in folder C:\MyXMLs, I guess you should have to add a line like `permission java.io.FilePermission "C:\\tmp\\*", "read"` – Davz Aug 24 '12 at 04:00
  • @Guitarroka Also please note that using the custom policy file trick is OK if you are only using the Applet for yourself. Remember that if you make this Applet public, every user that will want to use it will have to tweak its policy files... So in case you want to release this applet the best is that you digitally sign it and convince user to accept it when he will be prompted to do so. – Davz Aug 24 '12 at 04:06
  • How can I "digitally sign it"? – Nigh7Sh4de Aug 25 '12 at 00:23
  • @Guitarroka inside the link _edit policy files to allow it_ of my answer are two other links ([this one](http://support.sas.com/rnd/appdev/V30/tech/signing/RSASigning.htm) and [this one](http://mindprod.com/jgloss/signedapplets.html)) with some examples regarding applet signing – Davz Aug 25 '12 at 04:37