0

Below method return me below path

Output - "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;
    }

Now i run below command to export 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"

Exception - java.io.IOException: Cannot run program ""C:\ProgramData\MySQL\MySQL": CreateProcess error=2, The system cannot find the file specified

I also tried below, but same issue:-

1.) Process p = runtime.exec(batchCommand.split(" "));

2.) Adding double quotes like "C:\ProgramData\MySQL\MySQL Server 5.5\bin\"

Problem is it breaks as soon as it encounters space.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • use \\ or \ before the space "C:\\ProgramData\\MySQL\\MySQL\ Server 5.5\\bin\\" – Chetan chadha Aug 02 '17 at 14:12
  • Add double quote surronding the folder with spaces \"MySQL Server 5.5\" – Ori Marko Aug 02 '17 at 14:16
  • Have a look at [this](https://stackoverflow.com/questions/17141767/having-spaces-in-runtime-getruntime-exec-with-2-executables) and [this](https://stackoverflow.com/questions/6686592/runtime-exec-on-argument-containing-multiple-spaces) – phoenixSid Aug 02 '17 at 14:24
  • See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. Also **break a `String arg` into `String[] args` to account for things like paths *containing space* characters.** – Andrew Thompson Aug 02 '17 at 23:36

2 Answers2

0

From the Exception, it looks like JVM is not able to find MYSQL executable file at the location given.

This can be due to "\" file separator. Try using "\\" to separate your paths. Then your path would be : "C:\\ProgramData\\MySQL\\MySQL.

0

I'v written one class this might help you click 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();
}

}

Argha Das
  • 13
  • 7