Java - Runtime.exec()

윤준혁·2024년 4월 20일
1

언제 사용되는가?

  • Java Runtime Class : JVM이 작동하는 시스템 운영체제와의 인터페이스로 작동
  • Runtime.exec() 메소드를 사용하면 하나 이상의 추가 스레드 가능 작업이 작성됩니다. 추가 작업은 메소드에서 전달하는 명령 스트링을 처리합니다
  • 주로 운영체제 기반의 프로그램을 실행하거나 정보를 가져오는 기능을 수행
  • 시스템 침입의 주요 경로가 될 수 있으므로 실행 시 보안 요소를 고려해야 합니다

어떻게 사용하는가?

"Runtime.getRuntime().exec("{명령어}")"의 형태로 사용

명령어 유형

Runtime.exec()로 다른 Java 프로그램 호출 예시

import java.io.*;
 
public class CallHelloPgm
{
   public static void main(String args[])
   {
      Process theProcess = null;
      BufferedReader inStream = null;
 
      System.out.println("CallHelloPgm.main() invoked");
 
      // call the Hello class
      try
      {
          theProcess = Runtime.getRuntime().exec("java QIBMHello");
      }
      catch(IOException e)
      {
         System.err.println("Error on exec() method");
         e.printStackTrace();  
      }
        
      // read from the called program's standard output stream
      try
      {
         inStream = new BufferedReader(
                                new InputStreamReader( theProcess.getInputStream() ));  
         System.out.println(inStream.readLine());
      }
      catch(IOException e)
      {
         System.err.println("Error on inStream.readLine()");
         e.printStackTrace();  
      }
   } 
} 

Runtime.exec()로 CL 프로그램 호출 예시

import java.io.*;
 
public class CallCLPgm
{
   public static void main(String[] args)
   {
      try
      {
         Process theProcess =
                 Runtime.getRuntime().exec("/QSYS.LIB/JAVSAMPLIB.LIB/DSPJVA.PGM");
      }
      catch(IOException e)
      {
         System.err.println("Error on exec() method");
         e.printStackTrace();
      }
   } 
}

Runtime.exec()로 CL 명령 호출 예시

import java.io.*;
 
public class CallCLCom
{
   public static void main(String[] args)
   {
      try
      {
         Process theProcess =
            Runtime.getRuntime().exec("system DSPJVMJOB OUTPUT(*PRINT)");
      }
      catch(IOException e)
      {
         System.err.println("Error on exec() method");
         e.printStackTrace();
      }
   } 
} 

참조 문서

0개의 댓글

관련 채용 정보