简体   繁体   English

使用Java以提升权限运行Administrator的cmd

[英]Running cmd as Administrator with elevated rights using Java

I have elevated rights on my system ie, I can right click on cmd.exe and run it as Administrator. 我的系统具有提升的权限,即,我可以右键单击cmd.exe并以管理员身份运行它。 But when I do the same thing using Java, I am not able to do that. 但是,当我使用Java做同样的事情时,我无法做到这一点。 It asks my for Administrator password which I don't have. 它要求我提供我没有的管理员密码。

I am using following command: 我正在使用以下命令:

runas /profile /user:Administrator /savecred cmd /c start PATH\\to\\my\\file.bat

If you want to change the password of the Administrator, you can use this: 如果要更改管理员密码,可以使用以下命令:

public static void changeUserPassword(String username, String password) {
    try {
        Runtime.getRuntime().exec("net user "+username+" "+password);
        //This runs without needing Administrative Rights...
    } catch (IOException e) {}
}

But you can't run a jar file as Administrator, unless you are in the Administrator account. 但是,除非您具有管理员帐户,否则您不能以管理员身份运行jar文件。 Now, if you want a program you created to run as Administrator than I recommend you downloading Launch4j , this will turn your jar file into an exe file which will allow you to run it as Administrator... 现在,如果您希望创建的程序以管理员身份运行,而不是我建议您下载Launch4j ,它将把您的jar文件转换为exe文件,该文件将允许您以管理员身份运行它。

Launch4j Download Link: https://sourceforge.net/projects/launch4j/ Launch4j下载链接: https : //sourceforge.net/projects/launch4j/

Other than those ways that I listed, there is no way ( yet ) to auto-launch a file as Administrator... 除此之外,我列出的那些方法,也没办法( )到自动启动文件作为管理员...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM