13

I am developing a small shutdown scheduler project in which i have to put the computer in "Stand By" mode. The command that i am using is

Runtime.getRuntime().exec("cmd /c Powrprof.dll,SetSuspendState ");

This command requires Admin rights which i don't know how to get. Also while searching for previous answers i found i can use elevate.exe as

Runtime.getRuntime().exec("c:/elevate Rundll32.exe Powrprof.dll,SetSuspendState ");

Elevate.exe is doing the task but is consuming too much of time i.e. making the software slow. Is there any other speedy way? I am using Netbeans IDE.

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
  • [Run command line with Admin rights](http://stackoverflow.com/questions/9539978/running-windows-commandline-from-java-as-an-administrator-creating-a-manifest) – Jayamohan Jan 30 '13 at 04:23
  • The windows "shutdown" command might suit you better as it doesn't require admin access. To hibernate: shutdown /h – mikeslattery Jan 30 '13 at 04:26
  • @mikeslattery there is difference between standby and hibernate and i want to stand by. – Rohan Kandwal Jan 30 '13 at 04:39

4 Answers4

11
  Runtime.getRuntime().exec("runas /profile /user:Administrator \"cmd.exe /c Powrprof.dll,SetSuspendState\"");

Also plz see comments

Running as admin without Admin rights

Community
  • 1
  • 1
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • @TechExchange runas /user:Administrator cmd.exe /c Powrprof.dll,SetSuspendState will require to enter Administrator password which i donot have. – Rohan Kandwal Jan 30 '13 at 04:31
  • @TechExchange I don't have any passwords for my admin account and it asks for a password. – Rohan Kandwal Jan 30 '13 at 05:18
  • 1
    [Running as Admin without AdminPasswd](http://superuser.com/questions/244959/run-as-administrator-shortcut-without-password-prompt) – TheWhiteRabbit Jan 30 '13 at 05:19
  • sometimes the user has to be be specified as domain\user as seen in the runas help documentation (runas /?). Using this approach or running as Administrator didn't work for me, in a corporate environment, an account may have admin rights or if it doesn't it would not be possible to get Administrator passwords. In the former case, the command still didn't run. Running in elevated access level appears to be the solution – Deepak Jan 05 '18 at 11:52
10

You have a few options

A. Create a shortcut with admin priv.

The shortcut will run cmd /c Rundll32.exe Powrprof.dll,SetSuspendState

Your Java code will run the shortcut:

Runtime rt = Runtime.getRuntime();
rt.exec("cmd /c start \"\" \"myshortcut.lnk\"")

Right click the shortcut icon > properties > advanced > run as administrator

B. Run the java process as administrator

Again, create a shortcut and set to run as administrator. Any processes spawned will also have admin privileges. Your java code will run:

rt.exec("cmd /c Powrprof.dll,SetSuspendState")

C. Use JNA to directly call SetSuspendState routine. The Java process will require admin priv (like B), but you won't have to spawn a process. If you like this, I can provide source code.

D. Use wizmo utility: wizmo quiet standby

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
mikeslattery
  • 4,039
  • 1
  • 19
  • 14
  • `rt.exec("cmd /c start \"\" \"myshortcut.lnk\"")` but i have to make the shortcut as `cmd /c Rundll32.exe Powrprof.dll,SetSuspendState` but it takes a lot of time to execute the command. Is there any way by which i can increase the execution speed? – Rohan Kandwal Jan 30 '13 at 05:25
  • Ok one thing more if i want to use `sleep` option with `setsuspendstate`then i have to set `hibernation off`. This will again require a new admin shortcut and hence two popups for acceptance. Is there a way to achieve this task in 1 shortcut or command? – Rohan Kandwal Jan 30 '13 at 05:44
  • Wizmo working perfectly it even has more advance options. Thanks – Rohan Kandwal Jan 30 '13 at 06:23
  • is there any way to get the output of the command (in my case its w32tm /query /configuration) when using a shortcut/lnk? doesnt seem like it unfortunately – owen gerig Feb 17 '20 at 20:50
6

Add parameter /savecred

runas /profile /user:Administrator /savecred

Input the password one times. In future OS will not ask you password.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
makarey
  • 61
  • 1
  • 1
0

I'm using Windows 10. IDK why but runas isn't working and isn't reporting any errors.

I found this answer on superuser.com:

    powershell -Command "Start-Process 'cmd.exe /c Powrprof.dll,SetSuspendState ' -Verb runAs"
  • No password required if you have permission to elevate.
  • No shortcut required on client machine
  • No dependency on runas
  • Requires powershell

Powershell is installed by default on Windows since Windows 8 and Windows Server 2008 R2, according to an answer found on serverfault.com.

Jonathan
  • 949
  • 1
  • 11
  • 13