繁体   English   中英

从Java程序运行linux命令txl

[英]running linux command txl from java program

我知道我们可以在Java程序中运行linux终端命令,例如以下代码显示主目录的“ ls”命令。

String[] cmd1 =  {
        "/bin/sh",
        "-c",
        "cd ../.. && ls"};
        Process child1 = Runtime.getRuntime().exec(cmd1);
        child1.waitFor();

final BufferedReader is = new BufferedReader(new InputStreamReader(child1.getInputStream()));
        String line;
        while ((line = is.readLine()) != null) {

            System.out.println("output is: "+line);
        }

但是我不知道“ / bin / sh”和“ -c”是什么意思? 我在网上搜索它,有人用“ bash”或其他方式。 而且,如果仅以“ cd ../ .. && ls”运行命令,我将得到相同的结果。

因此,如果终端中有一个命令“ txl -o output inputFile”,其中“ txl”是安装的工具,那么如何用Java编写? 我试过了

String[] cmd1 =  {
        "/bin/sh",
        "-c",
        "cd ../.. && txl -o output inputFile"};

但是无法得到结果。 我添加“ cd ../ ..”首先从工作空间转到主目录,因为txl安装在主bin目录中。

谁能给我一些建议?

编辑:

我犯了一个愚蠢的拼写错误,这种格式的命令有效!

您可以在这里找到ls中每个参数的作用:

-c
 with -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime

为了在Java中执行程序并等待其完成,您可以使用本文

/bin/sh是一个程序,它是外壳程序,选项-c指定以下内容是要由外壳程序执行的命令(这是cmd1的第三行): cd ../.. && ls

编辑:

在这种情况下,必须执行shell,仅执行cd ../.. && ls行不通的,因为Java不能理解cdls之间的&&类的运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM