关于eclipse中运行配置中的classpath

时间:2013-07-05 14:55:07

标签: java eclipse classpath

Test1_Exec.java

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 -cp bin Test1");
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}

Test1.java:

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");
            fOut.close();
        }  catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Test1_Exec.class和Test1.class都在JavaTest(项目名称)下的bin文件夹中,代码可以正常工作。但我想通过添加bin文件夹"Process p = run.exec("java -cp bin Test1")"将代码"Process p = run.exec("java Test1")"替换为( right clikcing JavaTest(project name)->Run As->Run Configuration | Tab Classpath --- User Entries --- Advanced --- Add Folders ),然后Test1.txt不会被新代码创建。那么问题出在哪里?

1 个答案:

答案 0 :(得分:0)

对我而言,程序似乎不必要地复杂。为什么不在下面(如果你没有具体要求)

import java.io.IOException;

public class Test1_Exec {

    public static void main(String[] args) throws IOException {
        try {
            Test1.createFile();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test1 {
    public  static void createFile()
    {
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream("d:\\ppp\\Test1.txt");
            fOut.close();
        }  catch (IOException e) {
            e.printStackTrace();
        }

    }
}