developer tip

Java에서 Linux 쉘 명령을 호출하는 방법

copycodes 2020. 9. 12. 10:11
반응형

Java에서 Linux 쉘 명령을 호출하는 방법


리디렉션 (> &) 및 파이프 (|)를 사용하여 Java에서 일부 Linux 명령을 실행하려고합니다. Java는 어떻게 호출 csh하거나 bash명령 할 수 있습니까?

나는 이것을 사용하려고했다 :

Process p = Runtime.getRuntime().exec("shell command");

그러나 리디렉션 또는 파이프와 호환되지 않습니다.


exec는 쉘에서 명령을 실행하지 않습니다.

시험

Process p = Runtime.getRuntime().exec(new String[]{"csh","-c","cat /home/narek/pk.txt"});

대신.

편집 :: 내 시스템에 csh가 없어서 대신 bash를 사용했습니다. 다음은 나를 위해 일했습니다.

Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","ls /home/XXX"});

ProcessBuilder를 사용하여 공백 대신 명령과 인수를 구분하십시오. 이것은 사용 된 쉘에 관계없이 작동합니다.

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(final String[] args) throws IOException, InterruptedException {
        //Build command 
        List<String> commands = new ArrayList<String>();
        commands.add("/bin/cat");
        //Add arguments
        commands.add("/home/narek/pk.txt");
        System.out.println(commands);

        //Run macro on target
        ProcessBuilder pb = new ProcessBuilder(commands);
        pb.directory(new File("/home/narek"));
        pb.redirectErrorStream(true);
        Process process = pb.start();

        //Read output
        StringBuilder out = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null, previous = null;
        while ((line = br.readLine()) != null)
            if (!line.equals(previous)) {
                previous = line;
                out.append(line).append('\n');
                System.out.println(line);
            }

        //Check result
        if (process.waitFor() == 0) {
            System.out.println("Success!");
            System.exit(0);
        }

        //Abnormal termination: Log command parameters and output and throw ExecutionException
        System.err.println(commands);
        System.err.println(out.toString());
        System.exit(1);
    }
}

@Tim의 예제를 기반으로 자체 포함 된 메서드를 만듭니다.

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Shell {

    /** Returns null if it failed for some reason.
     */
    public static ArrayList<String> command(final String cmdline,
    final String directory) {
        try {
            Process process = 
                new ProcessBuilder(new String[] {"bash", "-c", cmdline})
                    .redirectErrorStream(true)
                    .directory(new File(directory))
                    .start();

            ArrayList<String> output = new ArrayList<String>();
            BufferedReader br = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
            String line = null;
            while ( (line = br.readLine()) != null )
                output.add(line);

            //There should really be a timeout here.
            if (0 != process.waitFor())
                return null;

            return output;

        } catch (Exception e) {
            //Warning: doing this is no good in high quality applications.
            //Instead, present appropriate error messages to the user.
            //But it's perfectly fine for prototyping.

            return null;
        }
    }

    public static void main(String[] args) {
        test("which bash");

        test("find . -type f -printf '%T@\\\\t%p\\\\n' "
            + "| sort -n | cut -f 2- | "
            + "sed -e 's/ /\\\\\\\\ /g' | xargs ls -halt");

    }

    static void test(String cmdline) {
        ArrayList<String> output = command(cmdline, ".");
        if (null == output)
            System.out.println("\n\n\t\tCOMMAND FAILED: " + cmdline);
        else
            for (String line : output)
                System.out.println(line);

    }
}

(테스트 예제는 디렉토리 및 하위 디렉토리의 모든 파일을 시간순으로 반복적으로 나열 하는 명령입니다 .)

By the way, if somebody can tell me why I need four and eight backslashes there, instead of two and four, I can learn something. There is one more level of unescaping happening than what I am counting.

Edit: Just tried this same code on Linux, and there it turns out that I need half as many backslashes in the test command! (That is: the expected number of two and four.) Now it's no longer just weird, it's a portability problem.

참고URL : https://stackoverflow.com/questions/1410741/how-to-invoke-a-linux-shell-command-from-java

반응형