Run command line (CMD) from java as administrator

I am working on a java project. For this project, administrator rights are required to execute any process on the system. To do this, is there a way to grant administrator rights when starting a project? Is there a way to run a command prompt with administrator privileges from java?

+1
java command admin prompt privileges
source share
2 answers

Disclaimer: I assume that you request this specifically for the Windows platform

I would recommend two ways:

Use runas

This is the easiest way, and since you really want a console window, the password hint does not look uncomfortable. On the other hand, the user may be suspicious of entering a password into the console window.

Use CreateProcessWithLogonW API Function

This requires the use of the Java Win32 library. (I would recommend JNA ). This is a bit more work, but it may lead to a better user experience due to the standard Windows login dialog.

+3
source share

You cannot simply request administrative privileges, and you cannot run admin-command-line from an application that has not been run with administrator privileges. This will be a serious security issue.

Instead, you can check to see if your application was run with administrator privileges, and if you do not prompt the user to do this (showing a little How-To on how to do this).

Unfortunately, this seam is becoming a more serious problem, since the standardized method does not apply to all platforms. However, this thread gives some ideas: Detecting if a Java application was running as a Windows administrator

+1
source share

All Articles