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

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

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 に答える