你的运行的命令写错了或者命令运行的时候有很多的输出 你没有得到 阻塞了import java.util.*;
import java.io.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Untitled1 {
public Untitled1() {
}
public static void main(String[] args) throws Exception {
Untitled1 untitled11 = new Untitled1();
System.out.println(Untitled1.runCommand("cmd /c dir"));
}
static public runCommand(String cmd) throws IOException {
java.lang.Process proc = Runtime.getRuntime().exec(cmd);
InputStream istr = proc.getInputStream();
BufferedReader br =
new BufferedReader(new InputStreamReader(istr));
String str;
while ((str = br.readLine()) != null)
System.out.println(str); try {
int i = proc.waitFor();
System.out.println(i);
}
catch (InterruptedException e) {
System.err.println("process was interrupted");
}
if (proc.exitValue() != 0)
System.err.println("exit value was non-zero"); // close stream
br.close();
}}
import java.io.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Untitled1 {
public Untitled1() {
}
public static void main(String[] args) throws Exception {
Untitled1 untitled11 = new Untitled1();
System.out.println(Untitled1.runCommand("cmd /c dir"));
}
static public runCommand(String cmd) throws IOException {
java.lang.Process proc = Runtime.getRuntime().exec(cmd);
InputStream istr = proc.getInputStream();
BufferedReader br =
new BufferedReader(new InputStreamReader(istr));
String str;
while ((str = br.readLine()) != null)
System.out.println(str); try {
int i = proc.waitFor();
System.out.println(i);
}
catch (InterruptedException e) {
System.err.println("process was interrupted");
}
if (proc.exitValue() != 0)
System.err.println("exit value was non-zero"); // close stream
br.close();
}}
import java.io.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class Untitled1 {
public Untitled1() {
} public static void main(String[] args) throws Exception {
Untitled1 untitled11 = new Untitled1();
Untitled1.runCommand("cmd /c dir");
} static public void runCommand(String cmd) throws IOException {
java.lang.Process proc = Runtime.getRuntime().exec(cmd);
InputStream istr = proc.getInputStream();
BufferedReader br =
new BufferedReader(new InputStreamReader(istr));
String str;
while ( (str = br.readLine()) != null)
System.out.println(str); try {
int i = proc.waitFor();
System.out.println(i);
}
catch (InterruptedException e) {
System.err.println("process was interrupted");
}
if (proc.exitValue() != 0)
System.err.println("exit value was non-zero"); // close stream
br.close();
}}
这个是正确的
p = r.exec("latex 1.tex", null, new File("c:/"));
java程序在等待本地进程运行结束.
jokerjava:
如你所写:
************************
try {
int i = proc.waitFor();
System.out.println(i);
}
catch (InterruptedException e) {
System.err.println("process was interrupted");
}
if (proc.exitValue() != 0)
System.err.println("exit value was non-zero");
*************************
i一直等于4,我想要的是,判断(进程是否结束)没有:什么也不做;结束:程序继续。
如果不是0
会打印exit value was non-zero
i会得到该进程运行结束后的返回值
一般正常结束会返回0,非正常结束返回非0值,
返回值是用类似java中System.exit(n)语句
或C/C++中exit(0)语句返回的