java getRuntime().exec 需要 UAC 的 exe
所以我们有一个作为 Windows 服务运行的 java 进程。它需要使用Runtime.getRuntime().exec(command)
执行命令。它执行的命令需要UAC。这是在 Windows Server 2008 上,听起来您无法为单个可执行文件禁用 UAC,那么还有其他方法可以实现此功能吗?
So we have a java process that runs as a windows service. It needs to execute a command with Runtime.getRuntime().exec(command)
. The command it executes requires UAC. This is on windows server 2008 and sounds like you cannot disable UAC for a single executable so is there any other way to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的 Java 应用程序作为 Windows 服务运行,它很可能在以下系统帐户之一下运行:SYSTEM(最有可能)、LOCAL SERVICE 或 NETWORK SERVICE。因此,如果服务在 SYSTEM 帐户下运行,则从该服务启动的所有内容都将继承该帐户。无论如何,您的服务必须被允许与桌面交互。
总而言之,如果您的进程以提升的方式运行,那么从它启动的进程也将以提升的方式运行。
要提升权限,您必须使用 ShellExecute 或Windows API 的 ShellExecuteEx 函数。如果您正在启动的 .exe 在其清单中标有
level=requireAdministrator
,则 shell 将显示 UAC 对话框。如果没有标记,您可以使用 runas 动词/操作强制 UAC 确认对话框。注意:Windows XP 上的runas
将显示“以其他用户身份运行”对话框。如果 Runtime.getRuntime().exec(command) 是通过 ShellExecute 实现的,那么用适当的清单标记 .exe 就可以了;如果
exec
使用 CreateProcess,该进程将以当前用户权限启动,即不提升;此外,如果 .exe 的清单中包含requireAdministrator
,则该进程根本不会启动。If your Java application runs as a windows service, it most likely runs under one of the system accounts: SYSTEM (most probable), LOCAL SERVICE, or NETWORK SERVICE. Thus if the service runs under SYSTEM account, everything you start from the service will inherit the account. Anyway your service must be allowed to interact with Desktop.
To summarize, if your process run as elevated, then processes started from it will also run elevated.
To elevate, you have to use ShellExecute or ShellExecuteEx functions of Windows API. If the .exe you're starting is marked with
level=requireAdministrator
in its manifest, the shell will display UAC dialog. If it's not marked, you can userunas
verb/operation to force UAC confirmation dialog. Note:runas
on Windows XP will show "Run as another user" dialog.If
Runtime.getRuntime().exec(command)
is implemented via ShellExecute, then marking the .exe with appropriate manifest will work; ifexec
uses CreateProcess, the process will be started with current user privileges, i.e. not elevated; moreover the process will not be started at all if .exe hasrequireAdministrator
in its manifest.