Windows:使用 CMD(或 Java)从非特权运行特权命令

发布于 2024-12-03 17:06:50 字数 275 浏览 0 评论 0原文

我将有一个以管理员身份运行并侦听端口的服务。我的 GUI 程序将与管理员服务对话以获取需要管理员权限的项目。

如果该服务尚未运行,我需要启动它。如何让我的 GUI 程序以管理员身份运行命令?我假设用户会被询问是否要继续。

我希望我可以在 CMD 窗口中输入一些内容,因为它应该非常适合我的 Java 程序。我正在考虑类似 run-as-admin javaw my-service.jar 的内容,其中 run-as-admin 是询问是否继续的命令。

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吾家有女初长成 2024-12-10 17:06:50

Windows 包含工具“runas”,可用于使用不同的用户帐户启动任何可执行文件。

然后在命令行上使用:

runas /user:Administrator "javaw -jar my-service.jar"

唯一的缺点是,您需要输入密码,不能将密码作为参数提供给 runas 命令

Windows contains the tool "runas" which can be used to start any executable with a different user account.

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

画▽骨i 2024-12-10 17:06:50

你这里有问题。非管理进程无法启动服务。最简单的事情就是安排服务自动启动。我强烈建议你走那条路。

您正在考虑做的事情将涉及创建一个包含 UAC 清单 将 requestedExecutionLevel 设置为 requireAdministrator。然后这个辅助应用程序就可以启动该服务。这样做的问题是,它要求登录的用户位于管理员组中,但您不能保证会出现这种情况。

总而言之,自动启动服务是首选。

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 manifest with 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.

All in all, starting the service automatically is to be preferred.

轻拂→两袖风尘 2024-12-10 17:06:50

您无法以管理员身份执行批处理文件。您需要创建该文件的快捷方式,然后在快捷方式“以管理员身份执行”中设置标志(如果这确实是您想要执行的操作)。

要从桌面执行此操作,请选择快捷方式,右键单击并选择“属性”->“快捷方式”选项卡->“高级”按钮。然后选中以管理员身份执行复选框。

要以编程方式执行此操作,请参阅 如何在 MSI 安装程序创建的快捷方式上设置“以管理员身份运行”标志

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

穿越时光隧道 2024-12-10 17:06:50

为此,我们使用 Wrapper 库:
http://sourceforge.net/projects/wrapper/

官方网站 目前似乎遇到动态 dns 问题,所以这里是回溯存档版本

从命令行(以及扩展为 Java),您可以将 Java 应用程序作为服务安装、卸载、启动和停止。

We use the Wrapper library for this:
http://sourceforge.net/projects/wrapper/

The official website seems to be suffering from dynamic dns issues at the moment, so here is the wayback archive version.

From the command line (and by extension, Java) you can install, uninstall, start, and stop your Java application as a service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文