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: www.phylosoft.org/forester
26 package org.forester.util;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
33 public class ExternalProgram {
35 public static boolean isExecuteableFile( final File path_to_cmd_f ) {
36 if ( !path_to_cmd_f.exists() ) {
39 else if ( path_to_cmd_f.isDirectory() ) {
42 else if ( !path_to_cmd_f.canExecute() ) {
47 private Process _process;
48 private final String _path_to_cmd;
50 public ExternalProgram( final String path_to_cmd ) {
51 final File path_to_cmd_f = new File( path_to_cmd );
52 checkCmdFile( path_to_cmd_f );
53 _path_to_cmd = path_to_cmd_f.getAbsolutePath();
56 private void checkCmdFile( final File path_to_cmd_f ) {
57 if ( !path_to_cmd_f.exists() ) {
58 throw new IllegalArgumentException( "[" + path_to_cmd_f.getAbsolutePath() + "] does not exist" );
60 else if ( path_to_cmd_f.isDirectory() ) {
61 throw new IllegalArgumentException( "[" + path_to_cmd_f.getAbsolutePath() + "] is a directory" );
63 else if ( !path_to_cmd_f.canExecute() ) {
64 throw new IllegalArgumentException( "[" + path_to_cmd_f.getAbsolutePath() + "] is not executeable" );
68 public InputStream getErrorStream() {
69 return getProcess().getErrorStream();
72 public InputStream getInputStream() {
73 return getProcess().getInputStream();
76 public OutputStream getOutputStream() {
77 return getProcess().getOutputStream();
80 private String getPathToCmd() {
84 private Process getProcess() {
88 public Process launch( final String[] opts ) throws IOException, InterruptedException {
90 if ( ( opts == null ) || ( opts.length < 1 ) ) {
91 cmd = new String[ 1 ];
94 cmd = new String[ opts.length + 1 ];
95 for( int i = 0; i < opts.length; i++ ) {
96 cmd[ i + 1 ] = opts[ i ];
99 cmd[ 0 ] = getPathToCmd();
100 System.out.println();
101 for( final String element : cmd ) {
102 System.out.print( element + " " );
104 System.out.println();
105 setProcess( Runtime.getRuntime().exec( cmd ) );
109 private void setProcess( final Process process ) {
113 public int waitFor() {
115 return getProcess().waitFor();
117 catch ( final InterruptedException e ) {
118 // TODO Auto-generated catch block
119 getProcess().destroy();