我在Windows 7上运行Eclipse(也发生在XP上)。 如下所示,路径保持不变,只有使用第二种技术(我需要使用)才能找到它。
这有效:
Runtime runtime = Runtime.getRuntime();
// Start street processes
for (int i=0; i < NUM_STREETS; i++) {
Process process = runtime.exec("java -classpath \\bin trafficcircle.Street");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null && !line.isEmpty()) {
System.out.println(line);
}
}
但这不是:
Runtime runtime = Runtime.getRuntime();
String[] cmdAndArgs = {"java -classpath \\bin trafficcircle.Street",
"",
"" };
// Start street processes
for (int i=0; i < NUM_STREETS; i++) {
Process process = runtime.exec(cmdAndArgs);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null && !line.isEmpty()) {
System.out.println(line);
}
}
并产生以下异常。为什么?感谢。
java.io.IOException: Cannot run program "java -classpath \bin trafficcircle.Street": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at trafficcircle.Main.runConfig(Main.java:81)
at trafficcircle.Main.main(Main.java:20)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
答案 0 :(得分:3)
我认为你需要单独放置args。否则它将寻找一个名为java -classpath...
的exe。像
String[] cmdAndArgs = {"java",
"-classpath",
"\\bin",
"trafficcircle.Street" };
要传递其他参数,只需将它们包含在数组
中即可String arg1 = ...;
String arg2 = ...;
String[] cmdAndArgs = {"java",
"-classpath",
"\\bin",
"trafficcircle.Street",
arg1,
arg2 };
答案 1 :(得分:0)
如果你没有传递任何参数,你可以尝试,
String[] cmdAndArgs = {"java -classpath \\bin trafficcircle.Street"}