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();
....
}上面代码没有在编译器中调试过,仅作参考:)
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();
....
}上面代码没有在编译器中调试过,仅作参考:)
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)
我在windows平台尝试了一下,在执行"cd"命令时报告error=2(即文件没有找到),可能是由于该命令需要调用windows命令解释器的原因!
不知道谁这样做过?有什么好的建议没有?
set CLASSPATH=%...
cd c:\testApp
java testApp aaa bbb (Look here)
写到*.cmd里
采用这个接口方法public Process exec(String[] cmdarray, String[] envp)应该是可行的,但是我不知道在子进程中如何获取到envp信息,有哪为大哥知道么?