日期:2014-05-20  浏览次数:20703 次

java 执行dos命令
run.exec("cmd.exe /c start tree");我想用java执行dos命令,但是dos命令懂的并不多,

我想显示当前目录下的树型结构,可是这样执行dos总是一闪而过,怎么让它不自动关闭,

有时候Java在dos下执行命令有时候窗口会自动关闭,有时候不会关闭呢,(比如打成jar包,会有一个dos窗口一闪而过)

求高手好好解释解释,在这里先谢过了。

急求,,,答案

------解决方案--------------------
我的程序


//用线程的方式读取DOS窗口的输出内容
class StreamDrainer implements Runnable {
private InputStream ins;
public StreamDrainer(InputStream ins) {
this.ins = ins;
}
public void run(){
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

//在dos下开始执行数据库备份与恢复
class DatabaseBR{

public DatabaseBR(){
}
public int StartRun(DatabaseBuckup databaseBuckup,String state,String statevalue)
{
int exitvalue=-1;
Runtime rt = Runtime.getRuntime();
Process processexp = null;

try{
//截取文件名称设置日志文件名
String file=" file=" + "'" + databaseBuckup.getFilepathname() + "'";
String logfilename=databaseBuckup.getFilepathname().substring(0,
databaseBuckup.getFilepathname().lastIndexOf("."));
//得到在dos下执行的命令
String exp = state + databaseBuckup.getDatabasename() + "/" + 
databaseBuckup.getDatabasepassword() + "@" + databaseBuckup.getNetname() + 
statevalue + file;
//开始执行命令
System.out.println(exp);
processexp=rt.exec(exp);
new Thread(new StreamDrainer(processexp.getInputStream())).start();
new Thread(new StreamDrainer(processexp.getErrorStream())).start(); 
processexp.getOutputStream().close();
//等待线程执行结束后返回
try {
exitvalue=processexp.waitFor();
System.out.println(exitvalue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(IOException e){
e.printStackTrace();
}

return exitvalue;
}
}
------解决方案--------------------
你把这些命令写在一个bat文件,使用
run.exec("cmd.exe /c start a.bat");
在bat文件中最后加上加上一个停顿的命令看看是否可以puse
------解决方案--------------------
我也想知道答案。。。
------解决方案--------------------
学习了!我也不明白!
------解决方案--------------------
探讨
这种效果吗?
Runtime.getRuntime().exec("cmd.exe /c start call tree /f");

------解决方案--------------------
你的分两次执行吧,第一次cmd,第二次tree,windows中要不关闭doc窗口是这样弄得。不知道java中这样行不行。
------解决方案--------------------
JDK 5.0的新特性 有执行本地方法的,好好去看。
------解决方案--------------------
Runtime.getRuntime().exec("cmd.exe /c start call tree /f");