以管理员身份运行命令提示符 [英] Run command prompt as Administrator
问题描述
我正在开发一个小的关闭调度程序项目,其中我必须把计算机放在Stand By
模式。我使用的命令是
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 ");
此命令需要管理员权限,我不知道如何获取。同时在搜索以前的答案,我发现我可以使用 elevate.exe
为
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
正在执行此任务,消耗太多的时间,即使软件慢。有什么其他的快速方式吗?我使用Netbeans IDE。
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.
推荐答案
您有几个选项
A。使用管理员priv创建快捷方式。
A. Create a shortcut with admin priv.
快捷方式将运行 cmd / c Rundll32.exe Powrprof.dll,SetSuspendState
您的Java代码将运行快捷方式:
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。以管理员身份运行java进程
B. Run the java process as administrator
再次创建快捷方式并设置为以管理员身份运行。任何生成的进程也将具有管理员权限。您的java代码将运行:
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。使用JNA直接调用SetSuspendState例程。 Java进程将需要admin priv(如B),但是你不必产生一个进程。如果你喜欢这个,我可以提供源代码。
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。 使用 wizmo 实用工具: wizmo安静待机
这篇关于以管理员身份运行命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!