runtime.exec(batchCommand) - 文件夹

时间:2017-08-02 14:04:20

标签: java mysql filepath runtime.exec

下面的方法让我回到路径

输出 - " C:\ ProgramData \ MySQL \ MySQL Server 5.5 \ bin \"

private String getPath() {
        String mysqlpath = "";

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(dbUrl, user, password);
            Statement stmt = con.createStatement();
            ResultSet res = stmt.executeQuery("select @@datadir");

            while (res.next()) {
                mysqlpath = res.getString(1);
            }

            mysqlpath = mysqlpath.replace("\\Data", "\\bin").replace("\\", "\\\\");
            System.out.println("Mysql path is :" + mysqlpath);
        } catch (Exception ee) {
            System.out.println(ee);
        }

        return mysqlpath;
    }

现在我运行以下命令导出mysql dump。

Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec(batchCommand);

batchCommand - " C:\ ProgramData \ MySQL \ MySQL Server 5.5 \ bin \ mysqldump" -h localhost --port 3306 -u root --password = root123 --add-drop-database -B accountingbook -r" E:\ softwares \ Tomcat_accountingBook \ webapps \ accountingbookaccountingbook-localhost-(02-08-2017 ).SQL"

异常 - java.io.IOException:无法运行程序"" C:\ ProgramData \ MySQL \ MySQL":CreateProcess error = 2,系统无法找到指定的文件

我也在下面尝试过,但同样的问题: -

1。)进程p = runtime.exec(batchCommand.split(""));

2.。)添加双引号,如" C:\ ProgramData \ MySQL \ MySQL Server 5.5 \ bin \"

问题是它遇到空间后就会中断。

2 个答案:

答案 0 :(得分:0)

从Exception看, JVM无法在给定的位置找到MYSQL可执行文件

这可能是由于“\”文件分隔符造成的。尝试使用“\\”分隔您的路径。然后你的路径是:“C:\\ ProgramData \\ MySQL \\ MySQL。

答案 1 :(得分:0)

我写了一节课,这可能会帮助您点击here

public class RuntimeExample {

private final String commandCode = "cmd /c";
private final Scanner scanner = new Scanner(System.in);
private Process process;
private BufferedReader reader;
private String input = "";
private final String defaultCode = "help";

public RuntimeExample() {
    UserInput();
}

private void UserInput() {
    System.out.println("Enter command (ex: ipconfig)");
    input = scanner.nextLine();
    if (input.isEmpty()) {
        System.out.println("You didn't give any command please take a look at these available Commands." + "\n");
        sendCommand(defaultCode);
    } else {
        sendCommand(input);
    }
}

private void sendCommand(String command) {
    try {
        process = Runtime.getRuntime().exec(commandCode + " " + command);
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            showResult(line);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

private void showResult(String result) {
    System.out.println(result + "\n");
}

public static void main(String[] args) {
    new RuntimeExample();
}

}