Process Watcher : ProcessBuilder « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » ProcessBuilder 




Process Watcher
    
/**
 
 *                        vpc-commons library
 *
 * Description: <start><end>
 *
 * Copyright (C) 2006-2008 Taha BEN SALAH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 
 * <pre>
 *      Process process = Runtime.getRuntime().exec(new String[]{"/bin/java","-version"}, null, new File("."));
 *      ProcessWatcher w = new ProcessWatcher(process, new ProcessWatcherHandler() {
 *          public void started(Process process) {
 *              System.out.println("Prcess started");
 *          }
 *
 *          public void stdout(Process process, String line) {
 *              System.out.println(line);
 *          }
 *
 *          public void stderr(Process process, String line) {
 *              System.err.println(line);
 *          }
 *
 *          public void ended(Process process, int value) {
 *              System.out.println("Process Shutdown. Exit Value :" + value);
 *          }
 *
 *          public void error(Process process, Throwable th) {
 *              System.err.println(th);
 *          }
 *      });
 *      w.start();
 * </pre>
 @author Taha Ben Salah ([email protected])
 * @creationtime 27 juin 2007 12:08:13
 */
public class ProcessWatcher {
    private Process process;
    private Thread end;
    private Thread out;
    private Thread err;
    private int result;
    private boolean stopped=false;
    private ProcessWatcherHandler handler;


    public ProcessWatcher(Process theProcess, ProcessWatcherHandler theHandler) {
        this.process = theProcess;
        this.handler = theHandler;
        end=new Thread(){
            @Override
            public void run() {
                try {
                    result = process.waitFor();
                    handler.ended(process,result);
                catch (Throwable e) {
                    handler.error(process,e);
                finally{
                    stopped=true;
                }
            }
        };
        out=new Thread(){
            @Override
            public void run() {
                String read;
                BufferedReader in=new BufferedReader(new InputStreamReader(process.getInputStream()));
                while (!stopped) {
                    try {
                        read = in.readLine();
                        if (read == null) {
                            break;
                        }
                        handler.stdout(process,read);
                    catch (Throwable e) {
                        handler.error(process,e);
                        break;
                    }
                }
            }
        };
        err=new Thread(){
            @Override
            public void run() {
                String read;
                BufferedReader in=null;
                try {
                    in = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    while (!stopped) {
                        try {
                            read = in.readLine();
                            if (read == null) {
                                break;
                            }
                            handler.stderr(process,read);
                        catch (Throwable e) {
                            handler.error(process,e);
                            break;
                        }
                    }
                catch(Throwable e) {
                    handler.error(process,e);
                finally {
                    if (in!=null) {
                        try {
                            in.close();
                        catch (IOException e) {
                            handler.error(process,e);
                        }
                    }
                }
            }
        };
    }

    public void start(){
        handler.started(process);
        end.start();
        out.start();
        err.start();
    }

    public int waitfor(){
        while(!stopped){
            Thread.yield();
        }
        return result; 
    }
}
/**
 
 *                        vpc-commons library
 *
 * Description: <start><end>
 *
 * Copyright (C) 2006-2008 Taha BEN SALAH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 */

/**
 @author Taha Ben Salah ([email protected])
 * @creationtime 27 juin 2007 12:08:46
 */
 interface ProcessWatcherHandler {
    public void started(Process process);
    public void stdout(Process process,String line);
    public void stderr(Process process,String line);
    public void ended(Process process,int value);
    public void error(Process process,Throwable th);
}

   
    
    
    
  














Related examples in the same category
1.ProcessBuilder Demo
2.Use ProcessBuilder.environment()
3.Use of ProcessBuilder that duplicates the functions of the DoRuntime example:
4.Exec Helper
5.Launches a process, redirecting the output of that sub-process to the output of this (the parent) process.
6.Execute a command
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.