2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.util;
28 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.util.ArrayList;
34 import java.util.List;
36 public class CommandProcessBuilder {
38 public static Process execute( final List<String> command, final File working_dir ) throws InterruptedException,
40 final ProcessBuilder builder = new ProcessBuilder( command );
41 if ( working_dir != null ) {
42 if ( !working_dir.exists() ) {
43 throw new IllegalArgumentException( "directory [" + working_dir.getAbsolutePath() + "] does not exist" );
45 if ( !working_dir.isDirectory() ) {
46 throw new IllegalArgumentException( "[" + working_dir.getAbsolutePath() + "] is not a directory" );
48 if ( !working_dir.canWrite() ) {
49 throw new IllegalArgumentException( "cannot write to [" + working_dir.getAbsolutePath() + "]" );
51 builder.directory( working_dir );
53 final Process process = builder.start();
57 public static void main( final String args[] ) {
58 final List<String> command = new ArrayList<String>();
59 command.add( System.getenv( "windir" ) + "\\system32\\" + "tree.com" );
62 System.out.println( "Directory : " + System.getenv( "temp" ) );
64 p = CommandProcessBuilder.execute( command, new File( System.getenv( "temp" ) ) );
65 final InputStream is = p.getInputStream();
66 final InputStreamReader isr = new InputStreamReader( is );
67 final BufferedReader br = new BufferedReader( isr );
69 while ( ( line = br.readLine() ) != null ) {
70 System.out.println( line );
72 System.out.println( "OK." );
74 catch ( final InterruptedException e ) {
77 catch ( final IOException e ) {