Windows:使用 CMD(或 Java)从非特权运行特权命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7322136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows: Running privileged command from non-privileged using CMD (or Java)
提问by Bryan Field
I will have a service that runs as administrator and listens on a port. My GUI program will talk to the administrator service for the items that require administrator privileges.
我将有一个以管理员身份运行并侦听端口的服务。我的 GUI 程序将与管理员服务对话以获取需要管理员权限的项目。
If the service is not yet running, I will need to start it. How can I get my GUI program to run a command as administrator? I assume the user will be asked if they want to continue.
如果该服务尚未运行,我将需要启动它。如何让我的 GUI 程序以管理员身份运行命令?我假设用户会被问到是否要继续。
I am hoping there is something I could type in a CMD window because that should fit very well into my Java program. I am thinking something like run-as-admin javaw my-service.jar
where run-as-admin
is the command that asks whether to continue or not.
我希望可以在 CMD 窗口中输入一些东西,因为它应该非常适合我的 Java 程序。我在想诸如询问是否继续的命令run-as-admin javaw my-service.jar
在哪里run-as-admin
。
采纳答案by Matthew Farwell
You can't execute a batch file as administrator. You need to create a shortcut to that file and then set the flag in the Shortcut 'Execute as Administrator', if this is really what you want to do.
您不能以管理员身份执行批处理文件。您需要创建该文件的快捷方式,然后在快捷方式“以管理员身份执行”中设置标志,如果这确实是您想要做的。
To do this from the desktop, select the Shortcut, right click and Properties->Shortcut tab->Advanced button. Then check Execute as Administrator checkbox.
要从桌面执行此操作,请选择快捷方式,右键单击和属性-> 快捷方式选项卡-> 高级按钮。然后选中以管理员身份执行复选框。
To do this programmatically, see How to set "Run as administrator" flag on shortcut created by MSI installer
要以编程方式执行此操作,请参阅如何在 MSI 安装程序创建的快捷方式上设置“以管理员身份运行”标志
回答by a_horse_with_no_name
Windows contains the tool "runas" which can be used to start any executable with a different user account.
Windows 包含工具“runas”,可用于使用不同的用户帐户启动任何可执行文件。
On the commandline you would then use:
在命令行上,您将使用:
runas /user:Administrator "javaw -jar my-service.jar"
The only drawback is, that you need to type in the password, you cannot supply the password as an argument to the runas command
唯一的缺点是,您需要输入密码,不能将密码作为参数提供给 runas 命令
回答by David Heffernan
You have a problem here. Non admin processes cannot start services. The very simplest thing to do is to arrange that the service starts automatically. I'd strongly recommend you take that route.
你这里有问题。非管理员进程无法启动服务。最简单的做法是安排服务自动启动。我强烈建议你走那条路。
What you are thinking of doing would involve creating a helper application that includes the UAC manifestwith requestedExecutionLevel
set to requireAdministrator
. This helper app could then start the service. The problem with this is that it requires that the logged on user is in the administrators group which you cannot guarantee will be the case.
你想这样做会涉及创建一个辅助应用程序,包括什么UAC清单与requestedExecutionLevel
设置为requireAdministrator
。然后这个助手应用程序可以启动服务。这样做的问题是它要求登录用户在管理员组中,您不能保证会是这种情况。
All in all, starting the service automatically is to be preferred.
总而言之,自动启动服务是首选。
回答by Charles Goodwin
We use the Wrapper library for this: http://sourceforge.net/projects/wrapper/
我们为此使用 Wrapper 库:http: //sourceforge.net/projects/wrapper/
The official websiteseems to be suffering from dynamic dns issues at the moment, so here is the wayback archive version.
该官方网站似乎是从动态DNS的问题痛苦的时刻,所以这里是韦巴克归档版本。
From the command line (and by extension, Java) you can install, uninstall, start, and stop your Java application as a service.
从命令行(以及扩展名 Java),您可以安装、卸载、启动和停止作为服务的 Java 应用程序。