0

I posted this question in Stack Overflow, and someone redirected me here:

Hello everyone. I'm doing a Windows Service in Java, this service list the files in a directory and write the results in a file. My problem is that when i call Java.File.isDirectory() when the service is running it always results false (It works well when i run the service manually as any other program). Besides, if i try the following: for(File F : directorio.listFiles()) trows an exception when i run the program as a service. I believe is permission related, because when i change the account in which the service is running to my own credentials it list the files correctly. Do you know if there is a workaround? (Change a windows policy, another kind of special account, another way to list the files of the directory, any other thing maybe i'm missing). Thanks in advance.

Rafael
  • 11
  • 2

2 Answers2

1

Thanks for all your help! I found my problem, i was reading the path from a configuration file which i assumed to be in the user's home (my home and System's home are not the same... my fault). Unfourtenly i didn't realize that because i was using a recycled class (:S). Again Thank you very much.

Rafael
  • 11
  • 2
0

Based on the documentation for File.isDirectory(), it sounds like you may have a security manager enabled. isDirectory requires the checkRead permission to be enabled, and you can test this yourself by using the something like the following (edit as needed):

SecurityManager security = System.getSecurityManager();
if(security)
    if(security.checkRead(FILE_PATH_HERE))
        // we have permission
    else
        // no permission

Adding the directory to the list of allowed directories may or may not be on your list of things to do. There are dozens of Google links with instructions if this is the case. The same goes if you would like to disable the SM completely.

Andrew M.
  • 10,982
  • 2
  • 34
  • 29
  • Another curious thing, i don't have a Security Manager (System.getSecurityManager() returns null) but i found that the Access Controller forbids me from reading a file no matter my credentials, this code always trows an exception: – Rafael Nov 01 '10 at 18:14
  • FilePermission perm = new FilePermission("MyDir", "read"); try { AccessController.checkPermission(perm); message="Allowed"; } catch (AccessControlException accessControlException) { message="Disallowed"+accessControlException.getMessage(); } – Rafael Nov 01 '10 at 18:15
  • Then it sounds like your user doesn't have file-system level access to the files in question. – Andrew M. Nov 02 '10 at 03:36
  • The strange thing is that this directory is in my home directory, and i'm running the program above with my own credentials. – Rafael Nov 04 '10 at 02:37