bricebanel
bricebanel

Reputation: 13

Applet needs to read an XML located outside the JAR

I know there already have been quite a few questions regarding this topic but I couldn't find one for my problem.

I'm simply creating a JAVA applet that reads from an xml file and then does what it has to do with it. When I test in in Eclipse, it works fine with the following code (week1 being a button!):

week1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
                parseXMLFile("../../week1.xml", teams);
                updateTable(teams);
          }
        });

As the .java file is in the src folder and the xml is at the root, this "../../week1.xml" does the trick. But when I export the applet as a JAR and insert it in an HTML page, the page loads with the applet but when I click the week1 button, nothing happens (I'm almost certain that it's because it does not find the xml file as it's outside the JAR archive). I get this error in the Java console:

java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.dir" "read")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPropertyAccess(Unknown Source)
at java.lang.System.getProperty(Unknown Source)
at java.io.Win32FileSystem.getUserPath(Unknown Source)
at java.io.Win32FileSystem.resolve(Unknown Source)
at java.io.File.getAbsolutePath(Unknown Source)
at java.io.File.getAbsoluteFile(Unknown Source)
at java.io.File.toURI(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at skullBall.parseXMLFile(skullBall.java:77)
at skullBall.access$0(skullBall.java:70)
at skullBall$1.actionPerformed(skullBall.java:52)

I would like to have the .jar archive and the xml file at the same location, how can I do this as the code inside the JAR needs to read the xml that's outside?

Thanks a lot

Upvotes: 1

Views: 1157

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

how can I do this as the code inside the JAR needs to read the xml that's outside?

  1. Put the XML in a path on the server where you can access it using your browser (check by trying to show it in a browser, via. the server/localhost).
  2. Access the resource by URL. To form that URL, use a path relative to the document base or code base of the applet.

E.G.

URL urlToXml = new URL(getDocumentBase(), "the.xml");

java.security.AccessControlException: access denied (
    "java.util.PropertyPermission" "user.dir" "read")

That should be solved by accessing the XML via URL relative to the document (or code) base.

Upvotes: 2

Cratylus
Cratylus

Reputation: 54074

Your question is confusing for me.
As I understand it the XML file is yours and it is expected to be always in the same location (i.e. the end-user is not expected to provide a different XML for example).
So why don't you deliver the file inside the jar so that you can use a relative path? Why are you trying to read it from outside the jar?

Upvotes: 1

Related Questions