用java的Runtime.getRuntime().exec(cmd)调用一个命令,该命令在cmd的窗口里执行没有问题,可以得到命令的输出结果,但是用Runtime.getRuntime().exec(cmd)方式调用却没有输出结果,关键输出代码:
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line= null;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
换成了,下面的代码执行找不到命令了,很奇怪,cmd里执行好好的!
Runtime rt = Runtime.getRuntime();
Process proc;
try {
proc = rt.exec("命令");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<error></error>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line= null;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
换成了,下面的代码执行找不到命令了,很奇怪,cmd里执行好好的!
Runtime rt = Runtime.getRuntime();
Process proc;
try {
proc = rt.exec("命令");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<error></error>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
解决方案 »
- 图片的src问题~! 我的路径为什么错的?
- 关于ServerSocket的一些疑问
- 自学J2SE遭遇瓶颈
- Integer类有一个整型和String型的构造方法 这句话是什么意思??
- 关于字符串获取问题。
- 求助制作简单WAP页面的方面的资料
- 布局管理器的问题
- 高手救我吧!!!!
- 为什么会出现这样的Exception?(Error occurred during initialization of VM;java/lang/ClassNotFoundException: sun/io/ByteToCharGBK)
- 如何从文件中读取中文(非unicode)到String中
- Mockito的@InjectMock失效?
- 我是JAVA新手,我安装了JDK1.7系统变量也配置好了,但是为什么我在运行窗口里输入javac却显示'javac'不是内部或外部命令
您如果在 PowerShell 运行,“命令”应该是“PowerShell”;
其它的通过参数传进去。
InputStream child_in = child.getInputStream();
String strReturn = "";while ((c = child_in.read()) != -1) {
strReturn += (char) c;
}
child_in.close();
你错了,Runtime.getRuntime().exec(cmd)这个才是执行命令,child.getInputStream()是获得返回的流................
InputStream child_in = child.getInputStream();
String strReturn = "";while ((c = child_in.read()) != -1) {
strReturn += (char) c;
}
child_in.close();
你错了,Runtime.getRuntime().exec(cmd)这个才是执行命令,child.getInputStream()是获得返回的流................
现在就是获取不到返回的流,我知道Runtime.getRuntime().exec(cmd)才是执行命令啊
这个exec和cmd不完全一样的。