In a Java application, I want to execute a jar file with several options. To do this, I build a list of strings with all the command elements and I pass it to the Runtime.exec()
method (It's really simple).
Here is the code with hard coded strings (The real code use variables of course):
List<String> cmd = new ArrayList<String>();
cmd.add("java");
cmd.add("-Dpython.path=\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\jython.jar\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\Lib\"");
cmd.add("-cp");
cmd.add("\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\..\\build\\jython-engine.jar\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\tools\\jython\\lib\\jython.jar\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\bin\\..\\plugins\\*\";\"C:\\Users\\ange\\Documents\\QTaste With Spaces\\bin\\..\\kernel\\target\\qtaste-kernel-deploy.jar\";testapi\\target\\qtaste-testapi-deploy.jar");
cmd.add("org.python.util.jython");
cmd.add("Testbeds\\ControlScripts\\playback.py");
cmd.add("start");
int exitCode = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]), env, output);
On Windows 8, when I do that inside the JAVA application, I get an error : "Could not find or load main class With". If I execute directly the command in a console, it works. I think this error is due to spaces in some paths, but I don't understand how to do more than surround with quotes all the strings with spaces (like I've already done).
This code works perfectly on Linux when the root directory contains (or not) spaces. This code works also on Windows 8 when the root directory does not contain spaces.
Do you have an idea on how to fix this problem?