首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从Java调用Linux shell命令

如何从Java调用Linux shell命令
EN

Stack Overflow用户
提问于 2009-09-11 13:05:19
回答 3查看 176.3K关注 0票数 95

我正在尝试使用重定向(>&)和管道(|)从Java执行一些Linux命令。Java如何调用cshbash命令?

我试着使用这个:

代码语言:javascript
复制
Process p = Runtime.getRuntime().exec("shell command");

但它与重定向或管道不兼容。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-09-11 13:12:15

exec不会在shell中执行命令

试一试

代码语言:javascript
复制
Process p = Runtime.getRuntime().exec(new String[]{"csh","-c","cat /home/narek/pk.txt"});

而不是。

我的系统上没有csh,所以我改用bash。下面的方法对我很有效

代码语言:javascript
复制
Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","ls /home/XXX"});
票数 102
EN

Stack Overflow用户

发布于 2009-09-11 13:12:17

使用ProcessBuilder分隔命令和参数,而不是空格。无论使用什么shell,这都应该可以正常工作:

代码语言:javascript
复制
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);
    }
}
票数 32
EN

Stack Overflow用户

发布于 2014-12-12 13:03:18

基于@Tim的示例构建一个自包含的方法:

代码语言:javascript
复制
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);

    }
}

(测试示例是command that lists all files in a directory and its subdirectories, recursively, in chronological order。)

顺便说一句,如果有人能告诉我为什么我需要四个和八个反斜杠,而不是两个和四个,我就能学到一些东西。还有比我所计算的更多的无法逃脱的发生。

编辑:我刚刚在Linux上尝试了同样的代码,结果发现test命令中的反斜杠数量只有原来的一半!(即:预期数量为2和4。)现在它不再是奇怪的了,这是一个可移植性的问题。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1410741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档