如何在具有管理员权限的java中运行cmd命令?

时间:2015-09-01 17:35:52

标签: java

我想从eclipse启动mysql服务器,我需要管理员权限来启动它,我该怎么做?

import java.io.*;

public class Clasa {
   public static void main(String args[]) {
        try {
            String command = "net start MySql55";
            ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", command);
            pb.inheritIO();
            Process pr = pb.start();
            int exitVal = pr.waitFor();
            System.out.println("Exited with error code " + exitVal);
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

This is really something that a scripting language (python, batch file) would be a better fit for. That being said, you can look here for more info on doing this in Java.