본문 바로가기
💻Tech/☕Java

[Java] 리눅스 명령어 실행 코드

by _viper_ 2019. 6. 11.
반응형

두 가지 방법입니다.

 

◾ ProcessBuilder 방식

  • ProcessBuilder는 명령어와 인수를 직접 지정하므로 쉘 명령어나 특수 문자(ex: |, >, < ) 사용이 불가 
String command = "ls";
new ProcessBuilder("/bin/bash", "-c", command).start();

 

Runtime.exec() 방식

    /**
     * cmd 명령어 실행
     *
     * @param cmd
     */
    public static boolean executeCmd(String cmd) {
        Process process = null;
        Runtime runtime = Runtime.getRuntime();
        StringBuffer successOutput = new StringBuffer();
        StringBuffer errorOutput = new StringBuffer();
        BufferedReader successBufferReader = null;
        BufferedReader errorBufferReader = null;
        String msg = null;
        boolean result = false;
 
        List<String> cmdList = new ArrayList<String>();
 
        cmdList.add("/bin/sh");
        cmdList.add("-c");
        
        cmdList.add(cmd);
        String[] array = cmdList.toArray(new String[cmdList.size()]);
 
        try {
        	Logger.info(CommonUtil.class, "cmd : " + cmd);
            process = runtime.exec(array);
 
            successBufferReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "EUC-KR"));
 
            while ((msg = successBufferReader.readLine()) != null) {
                successOutput.append(msg + System.getProperty("line.separator"));
            }
 
            errorBufferReader = new BufferedReader(new InputStreamReader(process.getErrorStream(), "EUC-KR"));
            while ((msg = errorBufferReader.readLine()) != null) {
                errorOutput.append(msg + System.getProperty("line.separator"));
            }
 
            process.waitFor();
 
            if (process.exitValue() == 0) {
                result = true;
            } else {
                Logger.error(CommonUtil.class, "abnormal finish error : " + successOutput.toString());
            }
 
            if (isNotBlank(errorOutput.toString())) {
                Logger.error(CommonUtil.class, "error : " + successOutput.toString());
            }
 
        } catch (IOException e) {
            Logger.error(CommonUtil.class, e.getMessage());
        } catch (InterruptedException e) {
            Logger.error(CommonUtil.class, e.getMessage());
        } finally {
            try {
                process.destroy();
                if (successBufferReader != null) successBufferReader.close();
                if (errorBufferReader != null) errorBufferReader.close();

            } catch (IOException e1) {
                Logger.error(CommonUtil.class, e1.getMessage());
            }
        }
        return result;
    }