검색결과 리스트
글
자바에서 외부파일 실행
java.lang
클래스 Runtime
java.lang.Objectjava.lang.Runtime
자바에서 외부파일을 실행시키기 위해 사용되는 Runtime클래스
오버로딩 되어잇는 exec()메소드
Process |
exec (String command) 지정된 캐릭터 라인 커멘드를, 독립한 프로세스로 실행합니다. |
Process |
exec (String [] cmdarray) 지정된 커멘드와 인수를, 독립한 프로세스로 실행합니다. |
Process |
exec (String [] cmdarray, String [] envp) 지정된 커멘드와 인수를, 지정된 환경을 가지는 독립한 프로세스로 실행합니다. |
Process |
exec (String [] cmdarray, String [] envp, File dir) 지정된 커멘드와 인수를, 지정된 환경과 작업 디렉토리를 가지는 독립한 프로세스로 실행합니다. |
Process |
exec (String command, String [] envp) 지정된 캐릭터 라인 커멘드를, 지정된 환경을 가지는 독립한 프로세스로 실행합니다. |
Process |
exec (String command, String [] envp, File dir) 지정된 캐릭터 라인 커멘드를, 지정된 환경과 작업 디렉토리를 가지는 독립한 프로세스로 실행합니다. |
void |
exit (int status) 현재 실행하고 있는 Java 가상 머신을, 종료 순서를 개시해 종료합니다. |
자바에서 특정 외부파일을 실행시키기 위한 예제입니다.
방법1.
import java.io.InputStreamReader;
public class ProcessTest {
public static void main(String args[]) throws Exception{
Process p=Runtime.getRuntime().exec("C:/1.exe");
BufferedReader bf=new BufferedReader(new InputStreamReader(p.getErrorStream()));
String str="";
while((str=bf.readLine())!=null){
System.out.println(str);
}
BufferedReader bf1=new BufferedReader(new InputStreamReader(p.getInputStream()));
String str1="";
while((str1=bf1.readLine())!=null){
System.out.println(str1);
}
}
}
위의 내용을 클래스화 시켜서 별도의 클래스로 작성!
public class Executor
{
public static void execute(String command)
{
try
{
String cmd[] = new String[3];
cmd[0] = "cmd.exe";
cmd[1] = "/C";
cmd[2] = command;
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
Process proc = rt.exec(cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
}
catch(Throwable t)
{
t.printStackTrace();
}
}
}
RECENT COMMENT