[英]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目录中。
谁能给我一些建议?
编辑:
我犯了一个愚蠢的拼写错误,这种格式的命令有效!
/bin/sh
是一个程序,它是外壳程序,选项-c
指定以下内容是要由外壳程序执行的命令(这是cmd1的第三行): cd ../.. && ls
)
编辑:
在这种情况下,必须执行shell,仅执行cd ../.. && ls
行不通的,因为Java不能理解cd
和ls
之间的&&
类的运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.