2013-07-05 77 views
0

Test1_Exec.java有關類路徑中運行配置在Eclipse

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都在bin文件夾下JavaTest(項目名稱) ,而代碼確實有效。但我想通過添加bin文件夾(right clikcing JavaTest(project name)->Run As->Run Configuration | Tab Classpath --- User Entries --- Advanced --- Add Folders)替換代碼"Process p = run.exec("java -cp bin Test1")""Process p = run.exec("java Test1")",那麼Test1.txt不是由新代碼創建的。那麼問題在哪裏?

回答

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

    } 
}