import java.io.*;
public class Test
{
public static void main(String [] args)
{
try
{
Process pro = Runtime.getRuntime().exec("cmd /c java -version");
pro.waitFor();
InputStream out = pro.getInputStream();
int i;
while ((i = out.read()) != -1)
{
System.out.println((char) i);
}
}
catch(IOException exception)
{
}
catch(InterruptedException exception2)
{
}
}
}
为何没有输出 谢谢
10 个解决方案
#1
这样看看:
BufferedReader br = new BufferedReader(pro.getInputStream());
String msg = null;
while((msg = br.readLine())! = null){
System.out.println(msg);
}
BufferedReader br = new BufferedReader(pro.getInputStream());
String msg = null;
while((msg = br.readLine())! = null){
System.out.println(msg);
}
#2
抱歉 pro.getInputStream() 报错 , BufferedReader(InputStream)构造函数没有定义
#3
不好意思,没注意,因为本机没有环境
这样就没问题了
BufferedReader br = new BufferedReader (new InputStreamReader(pro.getInputStream()));
String msg = null;
while ((msg = br .readLine()) != null) {
System.out.println(msg );
}
这样就没问题了
BufferedReader br = new BufferedReader (new InputStreamReader(pro.getInputStream()));
String msg = null;
while ((msg = br .readLine()) != null) {
System.out.println(msg );
}
#4
似乎就是没有输出
#5
?????????看不到你回的是啥
#6
那就是你的cmd 命令的事情了
你ping www.baidu.com看看
有没有输出。
你ping www.baidu.com看看
有没有输出。
#7
不好意思 是pro.waitFor();这句惹的事
现在好了 谢谢
现在好了 谢谢
#8
InputStream out = pro.getInputStream();
改成
InputStream out = pro.getErrorStream()
改成
InputStream out = pro.getErrorStream()
#9
process应该这么写:Process pro = Runtime.getRuntime().exec("cmd /c start java -version");
还有我这里也没有输出,还是第一次碰到,继续调试
还有我这里也没有输出,还是第一次碰到,继续调试
#10
to:楼上
exec("cmd /c start java -version"); 或者exec("cmd /c java -version"); 或者exec("java -version"); 应该都是一样的。
我刚才测试的代码:
public static void main(String[] args) {
try {
Process pro = Runtime.getRuntime().exec("cmd /c java -version");
pro.waitFor();
BufferedInputStream br = new BufferedInputStream(pro.getInputStream());
BufferedInputStream br2 = new BufferedInputStream(pro.getErrorStream());
int ch;
System.out.println("Input Stream:");
while((ch = br.read())!= -1){
System.out.print((char)ch);
}
System.out.println("Error Stream:");
while((ch = br2.read())!= -1){
System.out.print((char)ch);
}
} catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException e2) {
e2.printStackTrace();
}
}
执行结果:
Input Stream:
Error Stream:
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)
exec("cmd /c start java -version"); 或者exec("cmd /c java -version"); 或者exec("java -version"); 应该都是一样的。
我刚才测试的代码:
public static void main(String[] args) {
try {
Process pro = Runtime.getRuntime().exec("cmd /c java -version");
pro.waitFor();
BufferedInputStream br = new BufferedInputStream(pro.getInputStream());
BufferedInputStream br2 = new BufferedInputStream(pro.getErrorStream());
int ch;
System.out.println("Input Stream:");
while((ch = br.read())!= -1){
System.out.print((char)ch);
}
System.out.println("Error Stream:");
while((ch = br2.read())!= -1){
System.out.print((char)ch);
}
} catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException e2) {
e2.printStackTrace();
}
}
执行结果:
Input Stream:
Error Stream:
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)
#1
这样看看:
BufferedReader br = new BufferedReader(pro.getInputStream());
String msg = null;
while((msg = br.readLine())! = null){
System.out.println(msg);
}
BufferedReader br = new BufferedReader(pro.getInputStream());
String msg = null;
while((msg = br.readLine())! = null){
System.out.println(msg);
}
#2
抱歉 pro.getInputStream() 报错 , BufferedReader(InputStream)构造函数没有定义
#3
不好意思,没注意,因为本机没有环境
这样就没问题了
BufferedReader br = new BufferedReader (new InputStreamReader(pro.getInputStream()));
String msg = null;
while ((msg = br .readLine()) != null) {
System.out.println(msg );
}
这样就没问题了
BufferedReader br = new BufferedReader (new InputStreamReader(pro.getInputStream()));
String msg = null;
while ((msg = br .readLine()) != null) {
System.out.println(msg );
}
#4
似乎就是没有输出
#5
?????????看不到你回的是啥
#6
那就是你的cmd 命令的事情了
你ping www.baidu.com看看
有没有输出。
你ping www.baidu.com看看
有没有输出。
#7
不好意思 是pro.waitFor();这句惹的事
现在好了 谢谢
现在好了 谢谢
#8
InputStream out = pro.getInputStream();
改成
InputStream out = pro.getErrorStream()
改成
InputStream out = pro.getErrorStream()
#9
process应该这么写:Process pro = Runtime.getRuntime().exec("cmd /c start java -version");
还有我这里也没有输出,还是第一次碰到,继续调试
还有我这里也没有输出,还是第一次碰到,继续调试
#10
to:楼上
exec("cmd /c start java -version"); 或者exec("cmd /c java -version"); 或者exec("java -version"); 应该都是一样的。
我刚才测试的代码:
public static void main(String[] args) {
try {
Process pro = Runtime.getRuntime().exec("cmd /c java -version");
pro.waitFor();
BufferedInputStream br = new BufferedInputStream(pro.getInputStream());
BufferedInputStream br2 = new BufferedInputStream(pro.getErrorStream());
int ch;
System.out.println("Input Stream:");
while((ch = br.read())!= -1){
System.out.print((char)ch);
}
System.out.println("Error Stream:");
while((ch = br2.read())!= -1){
System.out.print((char)ch);
}
} catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException e2) {
e2.printStackTrace();
}
}
执行结果:
Input Stream:
Error Stream:
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)
exec("cmd /c start java -version"); 或者exec("cmd /c java -version"); 或者exec("java -version"); 应该都是一样的。
我刚才测试的代码:
public static void main(String[] args) {
try {
Process pro = Runtime.getRuntime().exec("cmd /c java -version");
pro.waitFor();
BufferedInputStream br = new BufferedInputStream(pro.getInputStream());
BufferedInputStream br2 = new BufferedInputStream(pro.getErrorStream());
int ch;
System.out.println("Input Stream:");
while((ch = br.read())!= -1){
System.out.print((char)ch);
}
System.out.println("Error Stream:");
while((ch = br2.read())!= -1){
System.out.print((char)ch);
}
} catch (IOException e) {
e.printStackTrace();
}catch (InterruptedException e2) {
e2.printStackTrace();
}
}
执行结果:
Input Stream:
Error Stream:
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)