以下两个文件,参考网上的资料,功能是在Test_Exec里调用Test.
我使用JB,在JB里点Test_Exec的"Run using Defaults",调用Test失败。
再在cmd窗口里,通过"java Test_Exec"来运行,调用Test成功!
请问为什么第一种方法不行,要如何才可以。谢谢!
///////////////////////////////////////
import java.io.*;public class Test_Exec
{
    public Test_Exec()
    {
        Runtime run = Runtime.getRuntime();
            try {
                Process p = run.exec("java Test");
            } catch (IOException ex) {
                System.out.println("FALSE");
            }
    }
    public static void main(String[] args) {
        Runtime run = Runtime.getRuntime();
        try {
            Process p = run.exec("java Test");
        } catch (IOException ex) {
            System.out.println("FALSE");
        }
    }
}
////////////////////////////////////import java.io.*;
public class Test
{
    public Test()
    {
        FileOutputStream fOut = null;
            try {
                fOut = new FileOutputStream("D:\\Test1.txt");
            } catch (FileNotFoundException ex) {
            }
            try {
                fOut.close();
            } catch (IOException ex1) {
            }
            System.out.println("被调用成功!");
    }
    public static void main(String[] args)
    {
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream("D:\\Test1.txt");        } catch (FileNotFoundException ex) {
        }
        try {
            fOut.close();
        } catch (IOException ex1) {
        }
        System.out.println("被调用成功!");
    }
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【changleqy】截止到2008-07-13 15:28:55的历史汇总数据(不包括此帖):
    发帖的总数量:42                       发帖的总分数:830                      每贴平均分数:19                       
    回帖的总数量:27                       得分贴总数量:1                        回帖的得分率:3%                       
    结贴的总数量:41                       结贴的总分数:810                      
    无满意结贴数:2                        无满意结贴分:40                       
    未结的帖子数:1                        未结的总分数:20                       
    结贴的百分比:97.62 %               结分的百分比:97.59 %                  
    无满意结贴率:4.88  %               无满意结分率:4.94  %                  
    值得尊敬
      

  2.   

    Process p = run.exec("java Test");  少了 cmd巴?
      

  3.   

    String[] t = new String[]{"cmd", "/c", "java", "Test"}; 
    Process p = run.exec(t);试试
      

  4.   

    同样代码构造方法和 main 方法里写两遍是什么意思?
      

  5.   


    我以为:main函数是为了在JB里点Test_Exec的"Run using Defaults",否则没有这项的
    构造方法是为了在cmd窗口里能通过java ***来运行,不然无法运行的。不知道理解的对不对
      

  6.   

    Test_Exec 能运行,但是Test不会被调用