1

I am working on a java project. This project need admin privileges to execute some process in the system. For this is there any way to give admin privileges while starting the project? Is there any way to start command prompt with admin privileges from java?

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
lankabeta
  • 91
  • 2
  • 7
  • 15
  • Related: [how to run a Java application as an administrator on Windows](http://stackoverflow.com/questions/1385866) – McDowell Oct 20 '11 at 08:29

2 Answers2

3

Disclaimer: I assume you are asking this specifically for the Windows platform

There are two ways I'd recommend:

Use the runas program

This is the easiest way and since you actually want a console window the password prompt doesn't look awkward. On the other hand a user might find it suspicious to enter his password into a console window.

Use the CreateProcessWithLogonW API function

This requires you to use a Win32 Java library. (I'd recommend JNA). It is a little more work but it might lead to a better user experience because of the standard Windows login dialog.

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
1

You can't simply request admin-privileges and you can't start an admin-command-line from an application that was not launched with admin-privileges. This would be a serious security problem.

Instead, you can check if your app was started with administrative privileges and if not prompt the user to do so (showing a little How-To on how to archive this).

Unfortunately this seams to be a bigger problem since there is no standardized way to do this covering all platforms. However this thread gives some ideas: Detect if Java application was run as a Windows admin

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111