Java – Getting ‘Access Denied’ when trying to access file from C:\ProgramData

javauacwindowswindows 7

Our clients are frequently reporting issue that they are getting "Access Denied" exception when they used to run application that has been installed with downloaded setup file. Our installer, installs executable on 'Program Files' and creates configuration files at C:\ProgramData. Once installation is done, our 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.

Yes.. "Run as Administrator" is the solution but we can not ask our all paid users to do such. I have searched option at can set privileges by some alternative way then manually run executable as "Run as Administrator".

I have found that help page but that's not working for me. My application is java desktop application so I have created .exe.manifest file and put that manifest file into executable where images and other product's properties files resides.

Manifest does not work for me and I am still getting "Access Denied" issue.

This is content of manifest file –

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
  <assemblyIdentity version="1.0.0.0"
     processorArchitecture="X86"
     name="<product_name>"
     type="win32"/> 
  <description>Description of your application</description> 
  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="true"/>
        </requestedPrivileges>
       </security>
  </trustInfo>
</assembly>

How I can attach the application manifest to the executable as I have only copied that manifest where images and other product's properties files resides ?

Is there any thing need to update in manifest file as I have copied as-is content except ?

Exception —

java.io.FileNotFoundException: C:\ProgramData\.<poduct_name>\config\<Product_Name>.xml (Access is denied) stacktrace javax.xml.transform.TransformerException: java.io.FileNotFoundException: C:\ProgramData\.<Product_Name>\config\<Product_Name>.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.<Product_Name>.main.dr.a(Unknown Source) at com.<Product_Name>.main.dr.a(Unknown Source) at com.<Product_Name>.main.dr.a(Unknown Source) at com.<Product_Name>.main.h.k(Unknown Source) at com.<Product_Name>.main.ay.run(Unknown Source) Caused by: java.io.FileNotFoundException: C:\ProgramData\.<Product_Name>\config\<Product_Name>.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\.<Product_Name>\config\<Product_Name>.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.<Product_Name>.main.dr.a(Unknown Source) at com.<Product_Name>.main.dr.a(Unknown Source) at com.<Product_Name>.main.dr.a(Unknown Source) at com.<Product_Name>.main.h.k(Unknown Source) at com.<Product_Name>.main.ay.run(Unknown Source) and the cause isjava.io.FileNotFoundException: C:\ProgramData\.<Product_Name>\config\<Product_Name>.xml (Access is denied)

Best Answer

The problem is because the user settings file is stored in the wrong place.

Only executable binaries and related resources belong in the ProgramData directory ("C:\Program Files..."), and this data should only change when a user with administrator rights installs or updates a program installation. Any configuration or data that the user needs to change must be kept in places where the user has rights to edit, such as their home folder, "My Documents", their HKEY_CURRENT_USER Registry key, or better yet, their AppData directory.

You can read more about these Windows programming requirements for user data storage here, and here is how some people have done this in Java. Hope that helps get you fixed up!

On second thought, perhaps using a tool like PROCMON.EXE would help narrow down the specifics of the denial--because it will show you whether the programming is opening the file/directory for reading, writing-with-all-permissions, trying to create a file that already exists, etc.

Related Topic