Search code examples
javaparsingdomfile-ioxerces

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


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)


Solution

  • 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").