简体   繁体   中英

about classpath in run configuration in eclipse

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 and Test1.class are both in the bin folder under JavaTest(project name), and the codes do work. But I want to replace the code "Process p = run.exec("java -cp bin Test1")" with "Process p = run.exec("java Test1")" by adding bin folder ( right clikcing JavaTest(project name)->Run As->Run Configuration | Tab Classpath --- User Entries --- Advanced --- Add Folders ) , then Test1.txt is not created by new codes. So where is the problem ?

To me program seemed unnecessarily complex. Why not below(if you dont have specific requirement)

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

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM