运行程序A,然后使用Runtime.getRuntime().exec("java -jar xx.jar")方式运行程序B,现在要A通知B关闭,不能使用socket
我使用了如下方法
程序A
BufferedWriter writer = null;
if (null != process) {
try {
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
writer.write("close");
writer.flush();
} catch (IOException e) {
LogManager.getInstance().logger_.debug(e.getMessage());
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
LogManager.getInstance().logger_.debug(e.getMessage());
}
}
}
}
程序B中
Thread thread = new Thread(new Runnable() {
public void run() {
BufferedReader inputReader = null;
try {
inputReader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while (null != (line = inputReader.readLine())) {
if (line.equals(CLOSE_COMMAND)) {
exitHistoryViewer();
}
}
} catch (IOException e) {
LogManager.getInstance().logger_.debug(e.getMessage());
} finally {
if (null != inputReader) {
try {
inputReader.close();
} catch (IOException e) {
LogManager.getInstance().logger_.debug(e.getMessage());
}
}
}
}
});
thread.start();
这样处理的后果就是画面OpenFileChooser的时候当掉,估计这里有问题
所以想知道还有社么其他方法可以实现A通知B关掉。

解决方案 »

  1.   

    i got some idea. You could check all the processes in the Windows Task Manager, find it and kill it by using TaskKill command.let me make a example for you soon.
      

  2.   

    所以想知道还有社么其他方法可以实现A通知B关掉。 
    A程序写命令到本地文件
    B程序从本地文件读取命令,执行相应的操作以上想法仅供参考
      

  3.   

    The first thing you need to do is to verify the Operation system by: 
    System.getProperty("os.name")Then, if it contains("Windows")Process proc = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq " + processName + "\"");
    to find that process you wannaThe last stuff you need to do is "Kill" it by
    Runtime.getRuntime().exec("taskkill /IM " + processName);
    That's all, easy hah
      

  4.   

    swandragon:外包方拒绝了我的建议。。
    coldanimal:你的方法值可以A关闭B,但是现在是A-》B-》C,A关掉要通知B和C关掉,你的方法值可以关掉B啊。。
    我死定了
      

  5.   

    A要通知B退出不是A强制B退出吧?
    如果只是杀掉进程,不用那么麻烦,直接destroy()就OK了
      

  6.   


    A强行关掉B,B关闭之前强行将C关掉可以吗?
      

  7.   

    A-b-c....A关闭的时候destory b,但这样就不能关闭c,这个是关键
      

  8.   

    A关闭B。不只能destory和kill吗,B再哪里关闭C啊
      

  9.   

    http://topic.csdn.net/u/20080827/19/3756c22f-36a9-4186-b8c7-ec3d773d932b.html
      

  10.   


    easy to implement.add a shutdownHook in B.Do the following in B:Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() { Thread.currentThread().setName("ShutDownHook Thread");
            KillC(); }
    });When you put it to B, then if the B is closed, it will execute the content in shutdownHook.