API中的Runtime类其中有个方法:
Process exec(String[] cmdarray, String[] envp, File dir)
在指定环境和工作目录的独立进程中执行指定的命令和变量。 这几个参数cmdarray,envp和dir是怎么取值的,我不是很懂,谁能给我举个例子啊
Process exec(String[] cmdarray, String[] envp, File dir)
在指定环境和工作目录的独立进程中执行指定的命令和变量。 这几个参数cmdarray,envp和dir是怎么取值的,我不是很懂,谁能给我举个例子啊
public void CmdMasm(String filepath,String filename ) {
boolean flag=false; //标志,当第一次记录已读到的":",变成true
int count=0; //记录":"的个数
String result=""; //保存编译后的结果
try {
String line;
/* String[] command = new String[5];
command[0] = "cmd.exe";
command[1] = "/c";
command[2] = "cd \""+filepath+"\"";
command[3] = "&";
command[4] = "E:\\examSys\\examClient\\visualassembly\\masm\\masm.exe";
Process p = Runtime.getRuntime().exec(command,null,new File("E:")); */ //正确
File path = new File(filepath);
String cmd ="cmd.exe /c E:\\examSys\\examClient\\visualassembly\\masm\\masm.exe";
Process p = Runtime.getRuntime().exec(cmd,null,path); System.out.println("filepath="+filepath); String str=filename+"\n";
OutputStream out = p.getOutputStream();
InputStream in = p.getInputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
// BufferedReader reader = new BufferedReader(new InputStreamReader(in));
InputStreamReader reader = new InputStreamReader(in);
char c[] = new char[1]; while( reader.read(c)!=-1 )
{
System.out.print(c);
if(count==4){
result=result + new String(c);
}
if(c[0]==':' && flag == true)
{ count=count+1;
System.out.println("\n");
writer.write("\n");
writer.flush();
}
if(c[0]==':' && flag == false)
{ flag = true;
count=count+1;
System.out.println(str);
writer.write(str);
writer.flush();
} }
flag= false;
reader.close();
writer.close();
}catch (Exception err) {
System.out.println("错误");
err.printStackTrace();
}
String element=""; //记录每个元素
Vector v = new Vector();
StringTokenizer tokenizer = new StringTokenizer(result.trim(), "\n\r", false);
while( tokenizer.hasMoreTokens() ){
element=tokenizer.nextToken();
if(element.trim()!="")
{
v.add(element);
// System.out.println(element);
}
}
System.out.println(v.elementAt(1).toString().trim());
System.out.println(v.elementAt(2).toString().trim());
}
public static void main(String [] args){
try{
Process process=Runtime.getRuntime().exec("p.exe");
process.waitFor();
System.out.println("DOS Sucess");
}catch(Exception e){
e.printStackTrace();
}
}}
看看这个,就差不多了,