반응형
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
public class RuntimeUtil {
public static String exec(String... command) throws IOException, InterruptedException {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
DefaultExecutor executor = new DefaultExecutor();
CommandLine cmdLine = CommandLine.parse(command[0]);
for (int i = 1, n = command.length; i < n; i++) {
cmdLine.addArgument(command[i]);
}
executor.setStreamHandler(psh);
executor.execute(cmdLine);
return stdout.toString();
}
}
반응형
'java' 카테고리의 다른 글
Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:3.1.0 (0) | 2019.12.09 |
---|---|
CustomPageable (0) | 2019.12.09 |
org.apache.http.ProtocolException: Target host is not specified (0) | 2019.12.09 |
프롤로그에서는 콘텐츠가 허용되지 않습니다. (0) | 2019.12.03 |
spring boot 설정 파일 vm옵션으로 외부에서 불러오기 (0) | 2019.11.20 |
Open JDK 사용하기 (0) | 2019.10.29 |
spring boot addResourceHandler not working (0) | 2019.10.29 |
java simpledate format (0) | 2019.08.22 |