JAVA/JAVA 일반

자바에서 window batch 파일 실행하기

this? 2015. 7. 14. 13:24
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String filePath = "D:/workspace/license/CodeTest/bin/test.bat";
		try {
		     
		    Process p = Runtime.getRuntime().exec(filePath);
		    p.waitFor();
		    
		    InputStream in = p.getInputStream();
		    ByteArrayOutputStream baos = new ByteArrayOutputStream();
		     
		    int c = -1;
		    while((c = in.read()) != -1)
		    {
		        baos.write(c);
		    }
		     
		    String response = new String(baos.toByteArray());
		    System.out.println("Response From Exe : "+response);
		    
		     
		} catch (Exception e) {
		    e.printStackTrace();
		}

	}
}

파일을 Runtime.getRuntime().exe() 에 경로를 입력하여 실행 시키고

프로세스 객체에서 출력값을 얻어 출력한다.

실행 후 메모장을 닫아야 출력값이 출력된다.