0

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();
        } 
    }
}

测试1.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

4

1 回答 1

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();
        }

    }
}
于 2013-07-05T15:01:47.300 回答