/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.engine;
import java.io.IOException;
import java.io.InputStream;
import org.apache.log4j.Logger;
/**
* Helper class to invoke system commands via static methods.
*
* @author Markus Kraetzig
*/
public final class SysRun {
private static final Logger log = Logger.getLogger(SysRun.class);
/**
* Creates a new process with an argument and returns immediately. This
* method could for example be used to call a viewer with a file argument.
* The viewer should be installed on the system.
*
* @param command
* name of the executable to run
* @param arg
* argument for the process to create
* @throws IllegalArgumentException
* if (command == null || command.length() == 0)
* @throws RuntimeException
* if an execute failed due to an IOException
*/
public static void invoke(String command, String arg) {
if (command == null || command.length() == 0)
throw new IllegalArgumentException("Command was empty.");
try {
Process proc = Runtime.getRuntime().exec(command + " " + arg);
handleInput(proc.getInputStream());
} catch (IOException e) {
throw new RuntimeException("Command \"" + command
+ "\" with argument \"" + arg + "\" failed.\n"
+ e.getMessage());
}
}
/**
* Deals with input of an invoked process.
*
* @param is
*/
private static void handleInput(final InputStream is) {
new Thread(new Runnable() {
public void run() {
try {
byte[] b = new byte[8];
int size = 0;
while ((size = is.read(b)) >= 0) {
log.info(new String(b, 0, size));
}
} catch (Throwable e) {
}
}
}).start();
}
}
Ads help maintain this website.