[code="java"]
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Stream extends Thread {
InputStream is;
String type;
Stream(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (type.equals("Error"))
System.out.println("ExecShell 执行SEHLL错误输出:" + line);
else {
System.out.println("ExecShell 执行SEHLL输出:" + line);
if(line.endsWith("ServerMain.vi_remotePort=9999")){
}
}
}
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
try {
Process pShell = Runtime.getRuntime().exec("cmd /C D:/work/gateway/script/StartLuServer.bat");
Stream outputGobbler = new Stream(pShell.getInputStream(), "Output");
Stream errorGobbler = new Stream(pShell.getErrorStream(), "Error");
outputGobbler.start();
errorGobbler.start();
pShell.waitFor();
Thread.sleep(200);
pShell.destroy();
} catch (Exception ex) {
System.out.println("ExecShell Exception:" + ex.getMessage());
}
}
}
[/code]
当执行bat到调用java后,程序就停住等待被调用的java执行完毕,问题是被调用的java程序是一个gateway性质,一直需要启动的程序,怎么让被调用的java仍然执行,而调用者继续执行下去?
你打开的gatway应该和你这个jvm平台没有关系了,那么你只要打开后就终止当前的线程,或者System.exit(0)强制退出,不管他了。这样索然所有的线程都终止了,但是你的gatway已经被打开了!
你的意思是你的子线程执行 bat文件后,main主线程不继续执行型下面的代码了吗?
这是我以前写的一个执行计算器的程序,你看看是否能帮助你
[code="java"]
public static void main(String[] args) {
System.out.println(System.getProperty("file.encoding"));
final String cmd="cmd /c calc";
//让一个子线程去执行cmd命令
Thread thread=new Thread(new Runnable(){
@Override
public void run() {
OSExecute.command(cmd);
}
});
thread.start();
System.out.println("继续执行。。。。");
}
[/code]
new Thread(new Runnable(){
@Override
public void run() {
OSExecute.command(cmd);
}
}).start(); 要另开个线程,不然会一直等待你执行.
这种情况不能满足。那个线程是由你这个主线程启动的,那么主线程结束的时候,那个线程也就结束了!