b5887a86ccf34f2c139208b0ec50b6b97a68d492
[jalview.git] / forester / java / src / org / forester / util / CommandProcessBuilder.java
1 // $Id:
2 // forester -- software libraries and applications
3 // for genomics and evolutionary biology research.
4 //
5 // Copyright (C) 2010 Christian M Zmasek
6 // Copyright (C) 2010 Sanford-Burnham Medical Research Institute
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.util;
27
28 import java.io.BufferedReader;
29 import java.io.File;
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;
35
36 public class CommandProcessBuilder {
37
38     public static Process execute( final List<String> command, final File working_dir ) throws InterruptedException,
39             IOException {
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" );
44             }
45             if ( !working_dir.isDirectory() ) {
46                 throw new IllegalArgumentException( "[" + working_dir.getAbsolutePath() + "] is not a directory" );
47             }
48             if ( !working_dir.canWrite() ) {
49                 throw new IllegalArgumentException( "cannot write to [" + working_dir.getAbsolutePath() + "]" );
50             }
51             builder.directory( working_dir );
52         }
53         final Process process = builder.start();
54         return process;
55     }
56
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" );
60         command.add( "/A" );
61         Process p;
62         System.out.println( "Directory : " + System.getenv( "temp" ) );
63         try {
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 );
68             String line;
69             while ( ( line = br.readLine() ) != null ) {
70                 System.out.println( line );
71             }
72             System.out.println( "OK." );
73         }
74         catch ( final InterruptedException e ) {
75             e.printStackTrace();
76         }
77         catch ( final IOException e ) {
78             e.printStackTrace();
79         }
80     }
81 }