Java:以管理员身份运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1385866/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:28:21  来源:igfitidea点击:

Java: run as administrator

javawindowsadminuacexe

提问by Martijn Courteaux

Is there a way in Java to ask the system to get control over administrator functionality. Of course without doing: Right click on the exe -> run as admin.
What I want is that there comes a frame from UAC like in Windows Vista or Windows 7.

Java 中有没有办法让系统控制管理员功能。当然不做:右键单击 exe -> 以管理员身份运行。
我想要的是像 Windows Vista 或 Windows 7 一样来自 UAC 的框架。

Or have I to do some settings while making an exe from the jar?

或者我在从 jar 制作 exe 时需要做一些设置吗?

采纳答案by ZippyV

You have to create a manifest file that specifies that your application needs administrator permissions. You can include the manifest in your exe or keep it as a separate file (yourapp.exe.manifest)

您必须创建一个清单文件,指定您的应用程序需要管理员权限。您可以在您的 exe 文件中包含清单或将其保存为单独的文件 (yourapp.exe.manifest)

http://msdn.microsoft.com/en-us/library/bb756929.aspx

http://msdn.microsoft.com/en-us/library/bb756929.aspx

回答by Yishai

ZippyV's answer is fine if you intend to launch the javaw.exe with system admin privileges, which if pure java code is what is getting blocked by the UAC (such as trying to write a file to a privileged directory), then that is what you will need to do.

如果您打算以系统管理员权限启动 javaw.exe,则 ZippyV 的答案很好,如果 UAC 阻止了纯 Java 代码(例如尝试将文件写入特权目录),那么这就是您将需要做。

If, however, you are trying to launch something external, but just with elevation, you could bundle an exe which elevates a command. Here is one.

但是,如果您尝试启动外部的东西,但只是提升,您可以捆绑一个提升命令的 exe。这是一个

回答by mathd

You can use a windows program to elevate your privilege. The program will show the UAC prompt and then you will have admin privilege.

您可以使用 Windows 程序来提升您的权限。该程序将显示 UAC 提示,然后您将拥有管理员权限。

http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/

http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/

You can then run for command like this:

然后你可以像这样运行命令:

Runtime.getRuntime().exec("Elevate.exe yourcommand");

回答by Matt DeVillier

The easiest way would be to use a wrapper to launch your JVM, then elevate the wrapper. I use a simple NSIS installer script with the UAC plugin to do this:

最简单的方法是使用包装器来启动您的 JVM,然后提升包装器。我使用带有 UAC 插件的简单 NSIS 安装程序脚本来执行此操作:

; Java Launcher
;--------------

; You want to change the below lines   
Name "my program"   
Caption "my program Launcher"    
Icon "iconfile.ico"    
OutFile "java launcher.exe"

; param below can be user, admin    
RequestExecutionLevel admin

!include UAC.nsh

SilentInstall silent
AutoCloseWindow true
ShowInstDetails show

Section ""    
  ; command to execute    
  StrCpy 
    String[] commands = {"cmd.exe", "/C" "mybatchfile.bat"};
    ProcessBuilder pb = new ProcessBuilder(commands);
    ..
    pb.start();
'javaw -jar myjarfile' SetOutPath $EXEDIR Exec
import java.io.IOException;

public class RunAsAdminExample {
    public static void main(String[] args) throws IOException {
        Process myappProcess = Runtime.getRuntime().exec("powershell.exe Start-Process <program.exe> -verb RunAs");
    }
}
SectionEnd

回答by Kaptkaos

I use the command line on windows for this purpose. From the Start menu, type cmdin the textbox and when the program cmd.exeappears, right click on it and select Run as Administrator. Now when ytou execute java -jar itwill run with Admin rights.

为此,我在 Windows 上使用命令行。在开始菜单的文本框中键入cmd,当程序cmd.exe出现时,右键单击它并选择以管理员身份运行。现在当你执行java -jar 时,它将以管理员权限运行。

回答by tomermes

Access java.exe and javaw.exe inside the installation folder. i.e - C:\Program Files\Java\jre7\bin or C:\Program Files\Java\jdk1.7.0_17\bin left click on them and select properties->compatability and there: check "run this program as administrator".

访问安装文件夹中的 java.exe 和 javaw.exe。即 - C:\Program Files\Java\jre7\bin 或 C:\Program Files\Java\jdk1.7.0_17\bin 左键单击它们并选择属性->兼容性,然后:选中“以管理员身份运行此程序”。

回答by crig

If you can run whatever you need to run from a windows CMD batch file.. You can create a batch file (on the fly even) from within your java app that does what you want and launch it. I have it working so my java program launches this and it pops up the normal windows UAC prompt to the user and then runs the commands if they choose YES.

如果您可以从 Windows CMD 批处理文件运行您需要运行的任何内容。您可以从 Java 应用程序中创建一个批处理文件(甚至即时),该文件可以执行您想要的操作并启动它。我让它工作,所以我的java程序启动它,它向用户弹出普通的Windows UAC提示,然后如果他们选择YES,则运行命令。

import java.io.IOException;

public class RunAsAdminExample {
    public static void main(String[] args) throws IOException {
        Process myappProcess = Runtime.getRuntime().exec("powershell.exe Start-Process notepad.exe -verb RunAs");
    }
}

Here is a thread that deals with requesting UAC access within a windows batch file: How to request Administrator access inside a batch file

这是一个处理在 Windows 批处理文件中请求 UAC 访问的线程: How to request Administrator access inside a batch file

NOTE: ProcessBuilder can be a little tricky to deal with, using a StreamGobbler to handle the output works for me: Handle Input using StreamGobbler

注意:ProcessBuilder 处理起来可能有点棘手,使用 StreamGobbler 来处理输出对我来说很有效: 使用 StreamGobbler 处理输入

回答by SourceSkyBoxer

I will explain how do you get elevated java application

我将解释你如何获得提升的 Java 应用程序

import java.io.IOException;

public class RunAsAdminExample {
    public static void main(String[] args) throws IOException {
        Process myappProcess = Runtime.getRuntime().exec("powershell.exe Start-Process -FilePath java.exe -Argument '-jar runasadmin.jar' -verb RunAs");
    }
}

program.exe is example just call notepad.exe

program.exe 只是调用 notepad.exe 的例子

##代码##

Than you have got elevated program. I recommand you - You need convert to binary wrapper like Launch4j or Parcle Than you can click - If you create custom installer (made by Java Swing or Awt.)

比你有提升的程序。我建议你 - 你需要转换成二进制包装器,比如 Launch4j 或 Parcle 比你可以点击 - 如果你创建自定义安装程序(由 Java Swing 或 Awt 制作。)

I hope you have good solution :) - If you use Linux or Mac OS X than you need use Parcle binary wrapper or other wrapper if you know...

我希望你有好的解决方案:) - 如果你使用 Linux 或 Mac OS X,那么你需要使用 Parcle 二进制包装器或其他包装器,如果你知道......

// EDIT good idea by Kaptkaos Than you write simple:

// 编辑 Kaptkaos 的好主意比你写的简单:

##代码##

Do not forget to use ''and ""if -jar <filename>.jarfrom -Argument of Start-Process. Note do not forget - If you use working directory and jar file must be in working directory example

不要忘记使用''and ""if -jar <filename>.jarfrom -Argument of Start-Process。注意不要忘记 - 如果您使用工作目录并且 jar 文件必须在工作目录示例中

  • .\myjar.jar
  • java .\RunAsAdminExample.class
  • .\myjar.jar
  • java .\RunAsAdminExample.class

Than you can see elevated java with argument :)

比您可以看到带有参数的提升的 java :)

Best regards!

此致!