如何在Java中执行其它程序
(2)使用线程
import java.util.*;import java.io.*;class StreamWatch extends Thread {InputStream is;String type;List output = new ArrayList();boolean debug = false;StreamWatch(InputStream is, String type) {this(is, type, false);}StreamWatch(InputStream is, String type, boolean debug) {this.is = is;this.type = type;this.debug = debug;}public void run() {try {PrintWriter pw = null;InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String line = null;while ((line = br.readLine()) != null) {output.add(line);if (debug)System.out.println(type + ">" + line);}if (pw != null)pw.flush();} catch (IOException ioe) {ioe.printStackTrace();}}public List getOutput() {return output;}}public class Test5 {public static void main(String args) {try {List list = new ArrayList();ProcessBuilder pb = null;Process p = null;// list the files and directorys under C:“list.add("CMD.EXE");list.add("/C");list.add("dir");pb = new ProcessBuilder(list);pb.directory(new File("C:““"));p = pb.start();// process error and output messageStreamWatch errorWatch = new StreamWatch(p.getErrorStream(),"ERROR");StreamWatch outputWatch = new StreamWatch(p.getInputStream(),"OUTPUT");// start to watcherrorWatch.start();outputWatch.start();//wait for exitint exitVal = p.waitFor();//print the content from ERROR and OUTPUTSystem.out.println("ERROR: " + errorWatch.getOutput());System.out.println("OUTPUT: " + outputWatch.getOutput());System.out.println("the return code is " + exitVal);} catch (Throwable t) {t.printStackTrace();}}}
7、在Java中执行Java程序
执行一个Java程序的关键在于:
(1)知道JAVA虚拟机的位置,即java.exe或者java的路径
(2)知道要执行的java程序的位置
(3)知道该程序所依赖的其他类的位置
举一个例子,一目了然。|||
(1)待执行的Java类
public class MyTest {
public static void main(String args) {
System.out.println("OUTPUT one");
System.out.println("OUTPUT two");
System.err.println("ERROR 1");
System.err.println("ERROR 2");
for(int i = 0; i < args.length; i++)
{
System.out.printf("args[%d] = %s.", i, args[i]);
}
}
}
(2)执行该类的程序
import java.util.*;
import java.io.*;
class StreamWatch extends Thread {
InputStream is;
String type;
List output = new ArrayList();
boolean debug = false;
StreamWatch(InputStream is, String type) {
this(is, type, false);
}
StreamWatch(InputStream is, String type, boolean debug) {
this.is = is;
this.type = type;
this.debug = debug;
}
public void run() {
try {
PrintWriter pw = null;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
output.add(line);
if (debug)
System.out.println(type + ">" + line);
}
if (pw != null)
pw.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public List getOutput() {
return output;
}
}
public class Test6 {
public static void main(String args) {
try {
List list = new ArrayList();
ProcessBuilder pb = null;
Process p = null;
String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
String classpath = System.getProperty("java.class.path");
// list the files and directorys under C:“
list.add(java);
list.add("-classpath");
list.add(classpath);
list.add(MyTest.class.getName());
list.add("hello");
list.add("world");
list.add("good better best");
pb = new ProcessBuilder(list);
p = pb.start();
System.out.println(pb.command());
// process error and output message
StreamWatch errorWatch = new StreamWatch(p.getErrorStream(),
"ERROR");
StreamWatch outputWatch = new StreamWatch(p.getInputStream(),
"OUTPUT");
// start to watch
errorWatch.start();
outputWatch.start();
//wait for exit
int exitVal = p.waitFor();
//print the content from ERROR and OUTPUT
System.out.println("ERROR: " + errorWatch.getOutput());
System.out.println("OUTPUT: " + outputWatch.getOutput());
System.out.println("the return code is " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
}