Jahanzaib
Jahanzaib

Reputation: 101

Running process as another user in java

Sorry I didn't clearly mention that I am working with windows, so the solutions related to linux Is there any way to execute a process through another user using java (by giving username and password in the code)?

Currently I am using process p = Runtime.getRuntime().exec("cmd") but I need to run this as a different user. Is there any function that supports this? or any other approach in java?

Edited: Sorry I didn't mention it previously that I am working on windows. So the solutions related to linux os are not applicable.

Upvotes: 1

Views: 4033

Answers (1)

Stephen C
Stephen C

Reputation: 718826

You need to call a command that takes care of the "run as other user" functionality. For example "su" or "sudo" on Linux1. This functionality is not availability from the JVM because:

  • it is inherently platform specific, and
  • it would be dangerous to support it in the JVM2.

There is also the issue that if it is risky to write (or use) programs that handle users' passwords on their behalf. Especially for users who don't understand and follow "best practice" in managing their passwords.


1 - ... though "sudo" has a different access control model.

2 - For a start, this functionality requires root privilege and a JVM running with root privilege is a major security risk.

Upvotes: 1

Related Questions