Reputation: 961
I need to call a bat file from my Java program and the path of the bat file contains folder having spaces in its name. I am able to escape these folder name but the problem is that the path of the bat file is coming from a variable and I don't know how to escape the variable values.
Path of the bat file:- C:/My Data/batfile.bat My code to escape the space:
This works fine
String t = "C://\"My Data\"//BatFile.bat";
Process process=Runtime.getRuntime().exec("cmd /c start /wait "+t);
This doesn't work
String t = path+"//BatFile.bat";
Process process=Runtime.getRuntime().exec("cmd /c start /wait "+t);
path variable in above line contains the bat file path "C:/My Data"
Suggestions ?
Upvotes: 0
Views: 1030
Reputation: 533530
You can do put it in quotes.
Process process=Runtime.getRuntime().exec("cmd", "/c", "start /wait \""+t+"\"");
Note: this doesn't work if the text also includes quotes, though a path shouldn't do so.
Upvotes: 4