簡體   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