I'm getting "Access is denied" when reading a file from my Java program on Windows Server 2012. I've been doing this sort of thing for years so I'm not a newbie. I just can't figure out what I'm missing!
Here is the stack (edited):
Caused by: java.io.FileNotFoundException: C:/ProgramData/MyProgram/resource/file.license (Access is denied)
#0: java.io.FileInputStream.open0(Native Method)
#1: java.io.FileInputStream.open(Unknown Source)
#2: java.io.FileInputStream.<init>(Unknown Source)
#3: java.io.FileInputStream.<init>(Unknown Source)
#4: com...util.FileUtil.readFileAsString(FileUtil.java:269)
The Java program is being run as a Windows Service using NSSM. The service is configured to run as user "cmb@contoso.com". The "file.license" file has user cmb@contoso.com with "Full" access. Domain "Users" group has Read, Read & Execute perms.
The perms on "C:/ProgramData/MyProgram" grant cmb@contoso.com Full access.
If I run Process Explorer and look at the "java.exe" Properties > Security I see that it shows "CONTOSO\cmb" as the user the process is running as.
I tried granting "Everyone" Read, Read & Execute perms on C:\ProgramData\MyProgram and on file.license but that had no effect.
If I run this same code direct, say from Eclipse, it works fine.
The readFileAsString method:
public static String readFileAsString(String filePath) {
if (filePath == null)
throw new IllegalArgumentException("No file argument given");
try {
byte[] buffer = new byte[(int) new File(filePath).length()];
FileInputStream f = new FileInputStream(filePath);
f.read(buffer);
f.close();
return new String(buffer);
} catch (IOException e) {
throw new OperationFailedException(e);
}
}
Java is 1.8_111 from Oracle
Process Monitor trace is shown in screenshot: https://drive.google.com/file/d/0B8BMXJDodRtpY19VekRaTkR5bTA/view
Process Monitor security properties of "java.exe" screenshot: https://drive.google.com/file/d/0B8BMXJDodRtpQko0YlVRNkZBQ1k/view