01: /*
02: JSmooth: a VM wrapper toolkit for Windows
03: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
04:
05: This program is free software; you can redistribute it and/or modify
06: it under the terms of the GNU General Public License as published by
07: the Free Software Foundation; either version 2 of the License, or
08: (at your option) any later version.
09:
10: This program is distributed in the hope that it will be useful,
11: but WITHOUT ANY WARRANTY; without even the implied warranty of
12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: GNU General Public License for more details.
14:
15: You should have received a copy of the GNU General Public License
16: along with this program; if not, write to the Free Software
17: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18:
19: */
20:
21: package net.charabia.jsmoothgen.application.gui.util;
22:
23: import java.io.*;
24: import java.util.*;
25:
26: /**
27: *
28: */
29: public class CommandRunner {
30: static private Vector s_runningprocs = new Vector();
31:
32: static {
33: Thread t = new ProcessCleaner();
34: t.setDaemon(true);
35: Runtime.getRuntime().addShutdownHook(t);
36: }
37:
38: static public class CmdStdReader implements Runnable {
39: InputStream m_in;
40:
41: public CmdStdReader(InputStream in) {
42: m_in = in;
43: }
44:
45: public void run() {
46: try {
47: BufferedReader br = new BufferedReader(
48: new InputStreamReader(m_in));
49: String line = null;
50: while ((line = br.readLine()) != null)
51: System.out.println(line);
52: } catch (Exception exc) {
53: exc.printStackTrace();
54: }
55: }
56:
57: }
58:
59: static public class ProcessCleaner extends Thread {
60: public void run() {
61: for (Enumeration e = s_runningprocs.elements(); e
62: .hasMoreElements();) {
63: Process p = (Process) e.nextElement();
64: try {
65: int res = p.exitValue();
66: } catch (Exception ex) {
67: p.destroy();
68: }
69: }
70: }
71: }
72:
73: static public void run(String[] cmd, File curdir) throws Exception {
74: Process proc = Runtime.getRuntime().exec(cmd, null, curdir);
75: InputStream stdin = proc.getInputStream();
76: InputStream stderr = proc.getErrorStream();
77:
78: new Thread(new CmdStdReader(stdin)).start();
79: new Thread(new CmdStdReader(stderr)).start();
80:
81: s_runningprocs.add(proc);
82: }
83: }
|