为什么Runtime.exec(“ls”)没有任何输出?
调用Runtime.exec方法将产生一个本地的进程,并返回一个Process子类的实例,该实例可用于控制进程或取得进程的相关信息。由于调用Runtime.exec方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO(如stdin,stdou,stderr)都通过Process.getOutputStream(),Process.getInputStream(),Process.getErrorStream()方法重定向给它的父进程了。用户需要用这些stream来向子进程输入数据或获取子进程的输出。所以正确执行Runtime.exec(“ls”)的例程如下:
try
{
process = Runtime.getRuntime().exec (command);
InputStreamReader ir=newInputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
String line;
while ((line = input.readLine ()) != null)
System.out.println(line);
}
catch (java.io.IOException e){
System.err.println (“IOException ” + e.getMessage());
}
try
{
process = Runtime.getRuntime().exec (command);
InputStreamReader ir=newInputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
String line;
while ((line = input.readLine ()) != null)
System.out.println(line);
}
catch (java.io.IOException e){
System.err.println (“IOException ” + e.getMessage());
}
【为什么Runtime.exec(“ls”)没有任何输出?】相关文章
1. 为什么Runtime.exec(“ls”)没有任何输出?
2. 若通过ObjectOutputStream向一个文件中多次以追加方式写入object,为什么用ObjectInputStream读取这些object时会产生StreamCorruptedExcepti
3. 软件测试LoadRunner面试题:If you want to stop the execution of your script on error, how do you do that?
4. 简述你对Statement,PreparedStatement,CallableStatement的理解
6. 软件测试LoadRunner面试题:Explain the following functions: – lr_debug_message
7. 我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串?
8. Java 中访问数据库的步骤?Statement 和PreparedStatement 之间的区别?
9. 既然说Ruby中一切都是对象,那么Ruby中类也是对象吗
10. 软件测试LoadRunner面试题: Types of Goals in Goal-Oriented Scenario
本文来源:https://www.mianshiwenti.com/a12573.html
进入下载页面
上一篇:线程问题:wait()方法是定义在哪个类里面
下一篇:若通过ObjectOutputStream向一个文件中多次以追加方式写入object,为什么用ObjectInputStream读取这些object时会产生StreamCorruptedExcepti
﹝为什么Runtime.exec(“ls”)没有任何输出?﹞相关内容
- 介绍一下ICMP(Internet Control Message Protocol)Internet控制信息协议
- 软件测试LoadRunner面试题:How do you identify the performance bottlenecks
- htmlentities() 和 htmlspecialchars()有什么区别
- 什么是Connection-oriented Protocol/Connectionless Protocol面向连接的协议/无连接协议
- Java如何调用外部Exe程序
- 软件测试LoadRunner面试题:What is think time? How do you change the threshold?
- 我有一个char * 型指针正巧指向一些int 型变量, 我想跳过它们。 为什么如下的代码((int *)p)++; 不行?
- 为什么如下的代码int a=100,b=100;long int c=a * b;不能工作
- 为什么UNION ALL比UNION快
- 什么叫应用程序域?什么是托管代码?什么是强类型系统?什么是装箱和拆箱?什么是重载?CTS、CLS和CLR分别作何解释?