Java에서 command line 입력을 실행시키기 위한 클래스입니다.
C/C++에서는 system(char *) 함수를 통해서 간단하게 실행시켰겠지만 Java에서는 클래스를 하나 만들어서 사용해야 합니다. 사용 예시는 main(String[]) 함수에서 보다시피 instance를 받아서 execute(String...) 함수로 명령어를 입력해주면 됩니다.package edu.cbnu.seal.utilities.cmd;
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;
/** * Process executor with using string commands * * @author Levdev * @version 1.0.0 * @since 2015.05.06 */ public final class CommandExecutor { /** * Tests {@code CommandExecutor} *
* @param args
* not used.
*/
public final static void main(final String... args) {
final String[] arguments =
{ "cmd", "/c", "dir" };
System.out.println(CommandExecutor.getInstance().execute(arguments));
}
// Singleton instance.
private final static CommandExecutor instance = new CommandExecutor();
/**
* Returns singleton instance.
*
* @return the instance of {@code CommandExecutor}.
*/
public final static CommandExecutor getInstance() {
return instance;
}
// Application directory
private final static File appdir = new File(System.getProperty("user.dir"));
// Working directory.
private File directory = null;
/**
* Constructs if only invoked by {@code CommandExecutor} class.
*/
protected CommandExecutor() {}
/**
* Sets the working directory.
*
* @param file
* new working directory.
* @return a reference to this object.
*/
public final CommandExecutor directory(final File file) {
this.directory = file;
return this;
}
/**
* Executes given commands.
*
* @param args
* commands.
* @return redirected texts.
*/
public final synchronized String execute(final String... args) {
return execute(false, args);
}
/**
* Executes given commands and waits.
*
* @param args
* commands.
* @return redirected texts.
*/
public final synchronized String executeAndWait(final String... args) {
return execute(true, args);
}
/**
* Executes given commands and waits if {@code waitTermination} is
* {@code true}
*
* @param waitTermination
* if {@code true}, execution will wait its termination, and if
* false, wont
* @param args
* commands.
* @return redirected texts.
*/
private final synchronized String execute(final boolean waitTermination,final String... args) {
if (args.length <= 0)
return null;
try {
final Process proc = startProcess(args);
if (waitTermination)
proc.waitFor();
return readInputStream(proc.getInputStream());
}
catch (final IOException | InterruptedException e) {
e.printStackTrace();
return null;
}
}
/**
* Returns a started {@code Process} instance.
*
* @param args
* commands.
* @return a instance of {@code ProcessBuilder}.
* @throws IOException
* if an I/O error occurs
*/
private final synchronized Process startProcess(final String... args)throws IOException {final File dir = (directory != null) ? directory : appdir;
return new ProcessBuilder(args).directory(dir).start();
}
/**
* Returns a {@code String} value read from {@code InputStream}
*
* @param in
* a stream to read any texts.
* @return texts read from stream.
* @throws IOException
* if an I/O error occurs
* @throws InterruptedException
* if the current thread is interrupted while waiting.
*/
private final synchronized String readInputStream(final InputStream in)throws IOException, InterruptedException {
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
final StringBuffer out = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append(System.lineSeparator());
}
return out.toString();
}
}
'Languages > Java' 카테고리의 다른 글
Java도 Hello World부터 - EasyCSVStream (0) | 2015.05.07 |
---|