的Runtime.exec(batchCommand) - 在文件夹空间问题

问题描述:

以下方法返回我下面路径的Runtime.exec(batchCommand) - 在文件夹空间问题

输出 - “C:\ ProgramData \ MySQL的\ MySQL服务器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转储。

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

batchCommand - “C:\ ProgramData \ MySQL的\ MySQL服务器5.5 \ BIN \ mysqldump的” -h本地主机--port 3306 -u根--password = root123 --add降数据库-B accountingbook -r “E:\软件\ Tomcat_accountingBook \的webapps \ accountingbookaccountingbook-localhost-(2017年2月8日)的.sql”

异常 - 产生java.io.IOException:不能运行程序 “” C :\ ProgramData \ MySQL \ MySQL“:CreateProcess error = 2,系统找不到指定的文件

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

1.)进程p = runtime.exec(batchCommand.split(“”));

2)添加双引号,如 “C:\ ProgramData \ MySQL的\ MySQL服务器5.5 \ BIN \”

问题是,它一旦遇到空间打破。

+0

使用\\或\前的空格“C:\\ ProgramData \\ MySQL \\ MySQL \ Server 5.5 \\ bin \\” –

+0

添加双引号surronding文件夹中的空格\“MySQL Server 5.5 \” – user7294900

+0

看看[this](https://stackoverflow.com/questions/17141767/having-s在运行时中的部分 - 执行与2可执行文件)和[this](https://stackoverflow.com/questions/6686592/runtime-exec-on-argument-containing-multiple-spaces) – phoenixSid

从例外情况看,它看起来像JVM无法在指定的位置找到MYSQL可执行文件

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

心中已经写了一类,这可能帮助你点击here

公共类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(); 
} 

}