[英]classpath with spaces for a java process launch with Runtime.getRuntime().exec() on Windows
在Java应用程序中,我想使用几个选项执行jar文件。 为此,我构建了一个包含所有命令元素的字符串列表,并将其传递给Runtime.exec()
方法(这很简单)。
这是带有硬编码字符串的代码(当然,实际代码使用变量):
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);
在Windows 8上,当我在JAVA应用程序中执行此操作时,出现错误:“无法找到或加载主类为”。 如果我直接在控制台中执行命令,那么它将起作用。 我认为此错误是由于某些路径中的空格引起的,但我不了解如何做更多的事情,而不是用引号引起来的所有字符串都用空格引起来(例如,我已经做过)。
当根目录包含(或不包含)空格时,此代码可在Linux上完美运行。 当根目录中不包含空格时,此代码也可在Windows 8上使用。
您对如何解决此问题有想法吗?
尝试逃脱空格:
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);
编辑
不要转义空格,而是将完整的类路径放在引号之间。
以下代码对我有用,如果我尝试编写错误的classPath,程序将打印java给出的错误
public static void main(String[] args) throws InterruptedException, IOException {
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");
System.out.println("START...");
Process p = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));
final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
new Thread(new Runnable(){
public void run() {
String line;
try {
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}).start();
final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
new Thread(new Runnable(){
public void run() {
String line;
try {
while ((line = err.readLine()) != null) {
System.err.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}).start();
int exitStatus = p.waitFor();
System.out.println("exit status: " + exitStatus);
}
我用简单的引号将-Dpython.path参数括起来,它有效...
public static void main(String[] args) throws InterruptedException, IOException {
List<String> cmd = new ArrayList<String>();
// String qtasteHome = "C:\\Users\\ange\\Documents\\QTaste With Spaces"; //RBA
// String qtasteHome = "/home/dev/workspaces/qtaste"; // UBUNTU
String qtasteHome = "D:\\Qtaste with spaces"; // Win 8.1
cmd.add("java");
cmd.add("-Dpython.path='" + qtasteHome + "\\tools\\jython\\lib\\jython.jar';'" + qtasteHome + "\\tools\\jython\\lib\\Lib'");
cmd.add("-cp");
cmd.add("\""
+ qtasteHome + "\\tools\\jython\\lib\\..\\build\\jython-engine.jar;"
+ qtasteHome + "\\tools\\jython\\lib\\jython.jar;"
+ qtasteHome + "\\bin\\..\\plugins\\*;"
+ qtasteHome + "\\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");
String command = "";
System.out.println("command parts :");
for ( String s : cmd)
{
System.out.println("\t" + s);
command += " " + s;
}
System.out.println("\nCommand : \n-------\n" + command + "\n-------");
System.out.println("START...");
Process p = Runtime.getRuntime().exec(
cmd.toArray(new String[cmd.size()]));
final BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream()));
new Thread(new Runnable() {
public void run() {
String line;
try {
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}).start();
final BufferedReader err = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
new Thread(new Runnable() {
public void run() {
String line;
try {
while ((line = err.readLine()) != null) {
System.err.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}).start();
int exitStatus = p.waitFor();
System.out.println("exit status: " + exitStatus);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.