簡體   English   中英

Java Runtime.exec()以運行Java程序

[英]Java Runtime.exec() to run a java program

這是情況。 我創建了一個UI,使使用基因編程系統(ECJ)的操作更加容易。

當前,您需要在ECJ文件夾中運行命令提示符,並使用與此類似的命令來執行參數文件。

java ec.Evolve -file ec\app\tutorial5\tutorial5.params

tutorial5的完整路徑在哪里

C:\Users\Eric\Documents\COSC\ecj\ec\app\tutorial5\tutorial5.params

並且命令提示符必須從

C:\Users\Eric\Documents\COSC\ecj

我的程序使用戶選擇一個.params文件(位於ecj子目錄中),然后使用Runtime.exec()執行

java ec.Evolve -file ec\app\tutorial5\tutorial5.params

我到目前為止有什么

// Command to be executed
String cmd = "cd " + ecjDirectory;        
String cmd2 = "java ec.Evolve -file " + executeDirectory;


System.out.println(cmd);
try {
    Process p = Runtime.getRuntime().exec(
                new String[]{"cmd.exe", "/c", cmd, cmd2});
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    statusTF.append(r.readLine());
    p.waitFor();        

    } catch (IOException | InterruptedException ex) {
        System.out.println("FAILED: " + ex.getMessage());
        statusTF.append("Failed\n");
    }

當前,它輸出更改目錄命令,但沒有其他輸出。 能做到嗎?

首先,首先不能由Runtime.exec()執行'cd'命令(請參閱如何使用Java運行時使用“ cd”命令? )。 您應該能夠在調用exec時為進程設置工作目錄(見下文)。

其次,運行“ cmd.exe / c”來執行您的過程不是您想要的。 您將無法獲得流程運行的結果,因為該結果將返回到命令窗口,該窗口將吞噬錯誤,然后關閉而不將錯誤傳遞給您。

您的exec命令應如下所示:

Process p = Runtime.getRuntime().exec(
    command, null, "C:\Users\Eric\Documents\COSC\ecj");

其中“命令”如下所示:

String command = "java ec.Evolve -file ec\app\tutorial5\tutorial5.params"

編輯:對於讀取錯誤消息,請嘗試以下操作:

String error = "";
try (InputStream is = proc.getErrorStream()) {
    error = IOUtils.toString(is, "UTF-8");
}
int exit = proc.waitFor();
if (exit != 0) {
    System.out.println(error);
} else {
    System.out.println("Success!");
}

每次對exec()調用都在新環境中運行,這意味着對cd的調用將起作用,但對下一次對exec()調用將不存在。

我更喜歡使用Apache的Commons Exec ,它提供了Java的Runtime.exec()的出色外觀,並提供了一種指定工作目錄的好方法。 另一個非常棒的事情是,它們提供了捕獲標准輸出和標准錯誤的實用程序。 這些可能很難正確地捕捉自己。

這是我使用的模板。 請注意,此示例期望退出代碼為0,您的應用程序可能有所不同。

String sJavaPath = "full\path\to\java\executable";
String sTutorialPath = "C:\Users\Eric\Documents\COSC\ecj\ec\app\tutorial5\tutorial5.params";
String sWorkingDir = "C:\Users\Eric\Documents\COSC\ecj";

try (
        OutputStream out = new ByteArrayOutputStream();
        OutputStream err = new ByteArrayOutputStream();
    )
{
    // setup watchdog and stream handler
    ExecuteWatchdog watchdog = new ExecuteWatchdog(Config.TEN_SECONDS);
    PumpStreamHandler streamHandler = new PumpStreamHandler(out, err);

    // build the command line
    CommandLine cmdLine = new CommandLine(sJavaPath);
    cmdLine.addArgument("ec.Evolve");
    cmdLine.addArgument("-file");
    cmdLine.addArgument(sTutorialPath);

    // create the executor and setup the working directory
    Executor exec = new DefaultExecutor();
    exec.setExitValue(0); // tells Executor we expect a 0 for success
    exec.setWatchdog(watchdog);
    exec.setStreamHandler(streamHandler);
    exec.setWorkingDirectory(sWorkingDir);

    // run it
    int iExitValue = exec.execute(cmdLine);

    String sOutput = out.toString();
    String sErrOutput = err.toString();
    if (iExitValue == 0)
    {
        // successful execution
    }
    else
    {
        // exit code was not 0
        // report the unexpected results...
    }
}
catch (IOException ex)
{
    // report the exception...
}

您可以使用Java processbuilderprocessBuilder文檔
您可以定義流程的工作目錄以及所有其他內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM