0

I was wondering How to execute java Application with different userName and password. For example: Right now: When I do,

System.getProperty("user.name"); 

I get user1.

But I want to program in such a way that it says user2.

Can somebody help me How to accomplish this with Java or bat files. Any kind of help is appreciated.

user234194
  • 1,623
  • 9
  • 35
  • 54

4 Answers4

6

The java system property user.name is set by the operating system. So if you login as a different user and start you're java program, it will run under that username.

But You can also change the user under which you execute an program (if you have sufficient rights for it!).

Linux: use the sudo command

sudo -u user2 java yourprogram

(but you need to have sudo rights, for example by being root)

Windows use the runas command:

runas /user:domain\user2 java yourprogram
Abhijith A C
  • 492
  • 1
  • 7
  • 21
Kdeveloper
  • 13,209
  • 11
  • 38
  • 48
  • could you please tell me where do I give the password for that user. – user234194 Sep 30 '10 at 18:30
  • @user234194 For Linux or Windows? If you're root in Linux you can do this without a password, the same thing applies when you configure sudo for regular users (see link). – Kdeveloper Sep 30 '10 at 18:34
  • in windows, i wanna do that in a single line. – user234194 Sep 30 '10 at 18:40
  • @user234194: Runas will always ask for a password. You could start you're program as a service though. – Kdeveloper Sep 30 '10 at 18:44
  • could you expalin little bit more please. I am new in this topic. thanks – user234194 Sep 30 '10 at 18:58
  • @user234194: There are however some scripts on the internet to circumvent this security feature of Runas. For example here (scroll way down): http://www.experts-exchange.com/Operating_Systems/WinXP/Q_20906129.html – Kdeveloper Sep 30 '10 at 19:01
  • @user234194: You could also take a look at: http://stackoverflow.com/questions/68113/how-to-create-a-windows-service-from-java-app – Kdeveloper Sep 30 '10 at 19:04
  • @user234194: And if it has to start from Batch, why don't you run it under the correct user? – Kdeveloper Sep 30 '10 at 19:11
  • `psexec` from [SysInternals](http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) (now part of Microsoft) will allow you to do the equivalent of runas with a username provided as an argument on the command line. It's intended for running code remotely, but works perfectly well for running local stuff too. – bacar Aug 13 '12 at 18:43
2

You can override this value, the same as any other system property with

java -Duser.name=my-new-user

or

System.setProperty("user.name", "my-new-user"); 

Note: neither solution changes the user-id of the process, just the value returned by System.getProperty("user.name");

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
1

You need to switch to user2 (su user2 on linux), then run your program.

Richard H
  • 34,219
  • 33
  • 105
  • 133
1

If you're doing this on Windows, you can use the runas command in a batch file to run in the context of a different user. On a Unix/Linux system, you can use the su command.

Jacob Mattison
  • 47,745
  • 8
  • 102
  • 120