Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Permission problem reading local XML file

Posted on 2007-11-28
10
Medium Priority
?
258 Views
Last Modified: 2012-05-05
Hi,

I am using the following in an applet:

Document doc;
try
{
    // Create a builder factory.
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false); // nah.
                  
    // Create the builder and parse the file.
    doc = factory.newDocumentBuilder().parse(new File("test01.xml"));
}

The file is test01.xml. I'm sure it exists in the same folder as my applet. When I run the applet, the java console prints out:


Exception in thread "AWT-EventQueue-27" 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 javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
      at Gallery.BuildFromXML(Gallery.java:41)
      at MarkGallery.renderInit(MarkGallery.java:45)
      at MarkGallery.render(MarkGallery.java:35)
      at BufferedApplet.update(BufferedApplet.java:57)
      at sun.awt.RepaintArea.updateComponent(Unknown Source)
      at sun.awt.RepaintArea.paint(Unknown Source)
      at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)


The same exact code works fine from a 'comman line' java application, reading text01.xml locally in the same folder. Any idea what I did wrong?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20371631
applets run inside a security sandbox which prevents access to the local disk
you need to sign the applet to access local disk
0
 
LVL 92

Expert Comment

by:objects
ID: 20371635
Or you could put the xml on the server and load it from there

0
 
LVL 92

Expert Comment

by:objects
ID: 20371645

eg.
    doc = factory.newDocumentBuilder().parse(getClass().getResourceAsStream("test01.xml"));

or

    URL xml = new URL(applet.getCodeBase(), "test01.xml");
    doc = factory.newDocumentBuilder().parse(xml.toString());
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DJ_AM_Juicebox
ID: 20371665
yeah it is running from the local server, in this case it happens to be the local directory of my desktop folder:

desktop/my_progject/index.html    // contains the applet
desktop/my_project/test01.xml   // the xml file
desktop/my_project/theapp.class  // the compiled java class file

so I'm just running it locally. I've done this plenty of times with bitmaps etc (this even worked via the command line version of the XML reader) but for some reason it doesn't like running the applet locally!
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20371674
Hmm switching to:

doc = factory.newDocumentBuilder().parse(getClass().getResourceAsStream("test01.xml"));

seemed to work.

Crazy!

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 20371675
doesn't matter that you're running locally, sandbox still applies.
The code you are using attempt to access local disk which an unsigned applet is not permitted to do.

0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20371679
ahh before I leave this question - once I upload all this stuff and run it from my server (the XML file will also be sitting locally on the server in the same folder as the applet) the:

     parse(getClass().getResourceAsStream("test01.xml"));

should still be valid right?

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 20371681
> Crazy!

not really, now you're reading it from the server instead of local disk.
(In your case they just happen to be the same place)


following covers signing applets if you need to go down that route.

http://mindprod.com/jgloss/signedapplets.html
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 20371693
> should still be valid right?

yes
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question