Runtime.getRuntime()
来源:互联网 发布:涨乐理财软件下载 编辑:程序博客网 时间:2024/05/15 23:46
- 权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://1504745673.blogbus.com/logs/77395808.html
假如要在java程序里执行一条linux可以用下面的写法Java代码
Processprocess=Runtime.getRuntime().exec(cmd);
process.waitFor();
Processprocess=Runtime.getRuntime().exec(cmd);process.waitFor();
但是当碰到像cd这样的命令时,上面的这种写法切不管用,很多时候我们以为是命令输错了或是文件找不到。
现在提供一种新的执行命令方法:Java代码
Filewd=newFile("/bin");
System.out.println(wd);
Processproc=null;
try{
proc=runtime.getruntime().exec("/bin/bash",null,wd);
}
catch(IOExceptione){
e.printStackTrace();
}
if(proc!=null){
BufferedReaderin=newBufferedReader(newInputStreamReader(proc.getInputStream()));
PrintWriterout=newPrintWriter(newBufferedWriter(newOutputStreamWriter(proc.getOutputStream())),true);
out.println("cd..");
out.println("pwd");
out.println("exit");
try{
Stringline;
while((line=in.readLine())!=null){
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch(Exceptione){
e.printStackTrace();
}}
Filewd=newFile("/bin");System.out.println(wd);Processproc=null;try{proc=runtime.getruntime().exec("/bin/bash",null,wd);}catch(IOExceptione){e.printStackTrace();}if(proc!=null){BufferedReaderin=newBufferedReader(newInputStreamReader(proc.getInputStream()));PrintWriterout=newPrintWriter(newBufferedWriter(newOutputStreamWriter(proc.getOutputStream())),true);out.println("cd..");out.println("pwd");out.println("exit");try{Stringline;while((line=in.readLine())!=null){System.out.println(line);}proc.waitFor();in.close();out.close();proc.destroy();}catch(Exceptione){e.printStackTrace();}}
- Runtime.getRuntime()
- Runtime.getRuntime()
- Runtime.getRuntime()
- Runtime.getRuntime()
- Runtime.getRuntime(). exec( )
- java Runtime.getRuntime().exec
- Runtime.getRuntime().addShutdownHook
- Runtime.getRuntime().addShutdownHook(Thread)
- 关于runtime.getruntime()
- Runtime.getRuntime().exec
- RunTime.getRunTime().addShutdownHook用法
- Runtime.getRuntime().exec()
- RunTime.getRunTime().addShutdownHook用法
- Runtime.getRuntime().exec()
- java Runtime.getRuntime().exec()
- Runtime.getRuntime().addShutdownHook(shutdownHook);
- Runtime.getRuntime.exec 注意事项
- Runtime.getRuntime().addShutdownHook
- Windows Azure安全概述
- c# 事件
- .NET调用SAP的WebService遇到的问题
- 对象在Hibernate中的三种状态
- guides.rubyonrails.org 读书笔记(五)
- Runtime.getRuntime()
- DLL木马的原理
- Runtime.getRuntime().exec();里面产生停滞(阻塞,blocking)
- Struts2下使用FreeMarker
- 水文过程线(二)
- 如何为毛笔效果
- 基于Linux的I2C驱动组成结构
- 中国红客真相,太牛了。
- Hibernate的数据检索策略(一)