public void runScript(String arg[]){
 BufferedString cmd = new   BufferedString ("startApp.cmd");
 if(arg!=null){
    for(int i = 0 ; i <arg.length ;i++){
      cmd.append(arg[i]);
    };
 }; Process p = Runtime.getRuntime().exec(cmd.toString());
 p.waitFor();
 
 .... 
}上面代码没有在编译器中调试过,仅作参考:)

解决方案 »

  1.   

    还有可以试试下面这个函数:public Process exec(String[] cmdarray,
                        String[] envp)
                 throws IOExceptionExecutes the specified command and arguments in a separate process with the specified environment. 
    Given an array of strings cmdarray, representing the tokens of a command line, and an array of strings envp, representing "environment" variable settings, this method creates a new process in which to execute the specified command. If envp is null, the subprocess inherits the environment settings of the current process. 
    Parameters:
    cmdarray - array containing the command to call and its arguments.
    envp - array of strings, each element of which has environment variable settings in format name=value. 
    Returns:
    a Process object for managing the subprocess. 
    Throws: 
    SecurityException - if a security manager exists and its checkExec method doesn't allow creation of a subprocess. 
    IOException - if an I/O error occurs 
    NullPointerException - if cmdarray is null 
    IndexOutOfBoundsException - if cmdarray is an empty array (has length 0).
    See Also:
    Process, SecurityException, SecurityManager.checkExec(java.lang.String)
      

  2.   

    你的第二种方法是可以考虑的,但是这样做的话代码就与平台相关了,例如在windows平台使用cd命令和在linux下使用需要不同的命令;
    我在windows平台尝试了一下,在执行"cd"命令时报告error=2(即文件没有找到),可能是由于该命令需要调用windows命令解释器的原因!
    不知道谁这样做过?有什么好的建议没有?
      

  3.   

    在你的脚本里直接输入就行了
    set CLASSPATH=%...
    cd c:\testApp
    java testApp aaa bbb   (Look here)
      

  4.   

    你可以用另一个程序来写这个文件,用输出流,把你需要的值传进去
    写到*.cmd里
      

  5.   

    这个我曾考虑过,但如果初始参数属于私密信息就不太可取,而且文件更新后再次运行时还要考虑恢复的问题!
    采用这个接口方法public Process exec(String[] cmdarray, String[] envp)应该是可行的,但是我不知道在子进程中如何获取到envp信息,有哪为大哥知道么?
      

  6.   

    同意 javafounder(漂流)的观点