javax.xml.transform.TransformerException: java.io.FileNotFoundException: <file_name>(Access is denied)

16,895

Solution 1

Sounds like file permissions on your XML files, doesn't it?

If you're executing from a web context, please bear in mind that the web user (for example, "nobody" under Linux/Apache, or "IUSR_MACHINE" under Windows/IIS) has MINIMAL privileges to access your filesystem.

And this is a Good Thing: especially if your application is exposed to the Internet :)

PS: Also, the directory path you cited doesn't look right:

C:\ProgramData.\config.xml

Are you sure it's not supposed to be "C:\Program Data\config.xml"????

PPS: While we're talking about "file permissions"; Windows Vista, Windows 7 and Server 2008 all have stricter rules against accessing anything in a drive's root (EX: "c:\") or system directories (EX: "c:\windows" or "c:\Program files").

Solution 2

Try changing it to use the file's URI.getPath() instead of just passing File object into StreamResult.

eg. StreamResult result = new StreamResult(anOutputFile.toURI().getPath());

Share:
16,895
Ashish Pancholi
Author by

Ashish Pancholi

learning..

Updated on June 08, 2022

Comments

  • Ashish Pancholi
    Ashish Pancholi almost 2 years

    I am getting exception at last line of code -

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DOMSource xmlSource = new DOMSource(document);
    
    StreamResult result;
    File f = new File(sFilePath);
    if (f.exists() == false) {
       result = new StreamResult(f);
    } else {
       result = new StreamResult(sFilePath);
    }
    
    transformer.transform(xmlSource, result);
    

    The exception stacktrace is -

    java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied) stacktrace javax.xml.transform.TransformerException: java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source) at com..main.dr.a(Unknown Source) at com..main.dr.a(Unknown Source) at com..main.dr.a(Unknown Source) at com..main.h.k(Unknown Source) at com..main.ay.run(Unknown Source) Caused by: java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream. (Unknown Source) at java.io.FileOutputStream. (Unknown Source) ... 7 more --------- java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream. (Unknown Source) at java.io.FileOutputStream. (Unknown Source) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source) at com..main.dr.a(Unknown Source) at com..main.dr.a(Unknown Source) at com..main.dr.a(Unknown Source) at com..main.h.k(Unknown Source) at com..main.ay.run(Unknown Source) and the cause isjava.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied)

  • Ashish Pancholi
    Ashish Pancholi almost 13 years
    My application is java desktop application. This application automatically run first time. Whenever user going to run it next time, it throws Access is denied as program can not able to access configuration files at C:\ProgramData.
  • paulsm4
    paulsm4 almost 13 years
    So are you saying 1) it always runs successfully the first time, but 2) it always fails the 2nd and subsequent times? That sounds like "file in use" error. Perhaps you're not closing all your files (in a "finally")? ALSO: look here: xml.apache.org/xalan-j/faq.html: "Why do I get 'fle not found'?"
  • Ashish Pancholi
    Ashish Pancholi over 11 years
    The issue is related file permission in "C:\Program Data\.."