windows java getRuntime().exec 一个需要 UAC 的 exe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5642892/
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
java getRuntime().exec an exe that requires UAC
提问by vinnyjames
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?
所以我们有一个作为 Windows 服务运行的 java 进程。它需要使用Runtime.getRuntime().exec(command)
. 它执行的命令需要 UAC。这是在 Windows Server 2008 上,听起来您无法为单个可执行文件禁用 UAC,那么还有其他方法可以使其工作吗?
回答by Alexey Ivanov
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.
如果您的 Java 应用程序作为 Windows 服务运行,它很可能在以下系统帐户之一下运行:SYSTEM(最有可能的)、LOCAL SERVICE 或 NETWORK SERVICE。因此,如果该服务在 SYSTEM 帐户下运行,则您从该服务启动的所有内容都将继承该帐户。无论如何,您的服务必须被允许与桌面交互。
To summarize, if your process run as elevated, then processes started from it will also run elevated.
总而言之,如果您的进程以提升的方式运行,那么从它启动的进程也会以提升的方式运行。
To elevate, you have to use ShellExecuteor ShellExecuteExfunctions 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 use runas
verb/operation to force UAC confirmation dialog. Note: runas
on Windows?XP will show "Run as another user" dialog.
要提升,必须使用Windows API 的ShellExecute或ShellExecuteEx函数。如果您启动的 .exelevel=requireAdministrator
在其清单中被标记,shell 将显示 UAC 对话框。如果它没有被标记,你可以使用runas
动词/操作来强制 UAC 确认对话框。注意:runas
在 Windows?XP 上将显示“以其他用户身份运行”对话框。
If Runtime.getRuntime().exec(command)
is implemented via ShellExecute, then marking the .exe with appropriate manifest will work; if exec
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 has requireAdministrator
in its manifest.
如果Runtime.getRuntime().exec(command)
通过 ShellExecute 实现,则使用适当的清单标记 .exe 将起作用;如果exec
使用CreateProcess,进程将以当前用户权限启动,即不提升;此外,如果 .exerequireAdministrator
在其清单中,则该进程根本不会启动。