Java App can't write on disk on Windows 7, why

2019-08-16 02:11发布

问题:

I wrote a Java app and I packaged it with NSIS (or NSYS...) installer. I works cleanly but sometimes I found that for some users running on Windows 7, the app won't be able to write on disk at all (it can't even write the log). The app is installed on c:\Programs (or d or e...). People from support say that they solve it by installing in c:\ . Why does this happen? Is it related to Java security? Maybe on my Windows installation and others I don't notice it because I have UAV turned off.

Ideas?

回答1:

You have hit the nail on the head when you wonder if it is because you have UAC switched off.

You should not be writing the log file to the location that the application is installed to, instead you will need to pick a location that is writable by the app, e.g. A directory below the user.home system property.

The reason that java applications can't do this and other applications can is related to Data Redirection, which causes writes to certain folders to be transparently redirected to a per-user store of data. I think this redirection is disabled for the JRE (checked using process explorer, which does not have a marking of 'Virtualized') which means that on java programs the transparent redirection will not happen.

If you want your application to be able to write into the install directory of the application, then the easiest way to accomplish this is to change the permissions of the folder on installation.



回答2:

The application apparently does not have write access to the location where tries to write the file. If it tries to write to the Program Files directory, that's to be expected (applications are not usually run with administrator rights). Instead write to a place which is guaranteed to be writable by the application/current user. For example get the Java property user.home which will point to the user's home directory, and write the files there in an appropriate subdirectory.



回答3:

I did some research and found in a different forum that Program Files is a protected directory for Windows 7 Home Premium, you have to install c:\ because of Windows Security. Just FYI

I was trying to code on Tomcat and found that I couldn't save files to Program Files. I had to install c:\Tomcat to be able to save any of my files.



标签: java security