import java.io.IOException;
public class Test1_Exec {
public static void main(String[] args) throws IOException {
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec("java Test1");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args)
{
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("invoked successfully");
}
}
问题是如果我在Eclipse中运行Test1_Exec,则不会创建Test1.txt并且不报告错误。但是如果我在命令窗口中键入“java Test1”,则会创建Test1.txt。 Test1_Exec.java和Test1.java位于同一个src文件夹中; Test1_Exec.class和Test1.class位于同一个bin文件夹中。那么Eclipse有什么问题?我的Eclipse版本是开普勒(20130614-0229)。代码不在eclipse中运行,但在命令窗口中运行
可能会发生这样的情况,即如果JAVA_HOME env变量包含空格,那么它会使JAVA_HOME env变量混淆。 – darijan