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版本是Kepler(20130614-0229)。

最佳答案

bin文件夹放入classpath

Process p = run.exec("java -cp path/to/bin Test1");


当前,java在项目目录中寻找Test1.class

10-07 19:06
查看更多