简体   繁体   中英

How run cmd in java program as administrator?

pleeas help me how can i run cmd in my java App as Administrator

I was able to extend my forward a bit but it was to ask me a password and there is no place to put a password

This picture shows

the picture :

在此处输入图片说明

import java.io.*;

public class CMD {

public static void main(String[] args) {

    try {
        Process p=Runtime.getRuntime().exec("runas /user:Administrator cmd");

        BufferedReader BR=new BufferedReader(new InputStreamReader(p.getInputStream()));
        String l;
        while((l=BR.readLine()) != null){
            System.out.print(l);

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

You can use the following command to execute

runas /profile /user:Administrator /savecred

In this scenario you will need to provide password once and in future jvm will not prompt you for password. You can provide password directly on the command window/eclipse console.

既然你有进程的句柄,就用进程的InputStream来读取密码提示,用OutputStream来写密码和一个“\\r\\n”来模拟在控制台输入密码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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