0

When I executed my java class it turned out that I don't have some privileges when I try to access a file and I've got false:

 file.canRead()

In order to get necessary rights I want to know who executes my code, I mean username who runs java class.

Sergii Shevchyk
  • 38,716
  • 12
  • 50
  • 61
  • Perhaps you can make them sign in before using your program? – Hovercraft Full Of Eels Jun 02 '12 at 20:21
  • @LouisWasserman The OP wants to know which user executed his code. Did you read the question? – Jeffrey Jun 02 '12 at 20:21
  • right. I need to grant rights. But I don't know whom. – Sergii Shevchyk Jun 02 '12 at 20:22
  • Possible duplicate: http://stackoverflow.com/questions/473446/java-current-machine-name-and-logged-in-user – Eelke Jun 02 '12 at 20:23
  • I don't agree. My code could be executed by someone else, not only logged user – Sergii Shevchyk Jun 02 '12 at 20:26
  • @berrouz Is this a web application? If so, it doesn't make sense to get the username. The username will always be of the user that the web server is running under. It will NOT be the username of the person that is actually using the web application. – Michael Jun 02 '12 at 21:54
  • Michael, I would like to know whether Java has any method to check who exactly executed my code or not. Something like `System.getProperty("user.name");` The last command says who currently logged in but I want to know who runs my code. – Sergii Shevchyk Jun 03 '12 at 15:33

3 Answers3

1

In a (desktop or web) application it would be simply:

System.getProperty("user.name");

A web application could be run under another user (with care).

In a WebStart application, run over the internet, there is a sandbox, i.e. asking the client to allow file system access.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

While this may answer your question, it seems a bit silly to base your program off of who is logged in via your OS. You can find system information by doing calls like this: System.getProperty(nameOS));

This site should have all the calls you need. http://www.roseindia.net/java/beginners/OSInformation.shtml

Max
  • 5,799
  • 3
  • 20
  • 27
0

Current logged in user will be the default user.

If the logged user can acces that file, then java application can also acces.

David Mathias
  • 296
  • 3
  • 12