반응형

ShellCommandTest

public class ShellCommandTest {
    @Test
    public void test() {
        List<String> result1 = runCommand(".", "cmd /c dir");
        List<String> result2 = runCommand(".", "git status");

        System.out.println(result1);
        System.out.println(result2);
    }

    private List<String> runCommand(String workDir, String command) {
        List<String> result = new ArrayList<>();

        try {
            Process process = Runtime.getRuntime().exec(command, null, new File(workDir));

            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line = "";
                while ((line = reader.readLine()) != null) {
                    result.add(line);
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }

        return result;
    }
}

참고

반응형

'Development > Java' 카테고리의 다른 글

[Java] Hash Algorithm  (0) 2021.11.17
[Java] Stream  (0) 2021.09.09
[Java] Date  (0) 2021.07.24
[Java] CompletableFuture  (0) 2021.03.30
[Java] Annotation Processor  (0) 2021.03.04

+ Recent posts