IT/개발

[Java] Linux 명령 날리고 결과 값 리턴 받기

알콩달콩아빠 2023. 8. 17. 19:51
728x90
반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LinuxCommander {
    public static String exec(String command){
        String result = "";
        Runtime rt = Runtime.getRuntime();
        Process p = null;
        StringBuffer sb = new StringBuffer();
        try{
            p=rt.exec(command);
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String cl = null;
            while((cl=in.readLine())!=null){
                sb.append(cl);
            }
            result = sb.toString();
            in.close();
        }catch(IOException e){
            e.printStackTrace();
            return "";
        }
        return result;
    }
}

 

728x90
반응형