新线程中的Java执行命令

时间:2018-07-10 16:43:53

标签: java multithreading process command

我正在尝试将“ mvn -version”命令发送到特定目录。但是Process(代码中的AgentProcess)以值127退出。当我在终端窗口中触发同一命令时,它会给我结果。我正在Mac OS中执行此操作。

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import com.sun.prism.impl.Disposer.Record;

public class ProgressScreen{

   public static void main(String[] args) {
       try {
        checkMaven("/Users/harshit.shihora/Documents/MyProject");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch(Exception e) {
        e.printStackTrace();
    }
   }

   public static Boolean checkMaven(String projectPath) throws IOException, InterruptedException {

        Process AgentProcess = null;
        Boolean status = false;

        String command = "mvn -version ";

        if (System.getProperty("os.name").contains("Windows") || System.getProperty("os.name").contains("windows")) {

            String[] cmd = { "cmd", "/C", command };
            AgentProcess = Runtime.getRuntime().exec(cmd, null, new File(projectPath));
        } else {

            String[] cmd = { "/bin/bash", "-c", command };
            AgentProcess = Runtime.getRuntime().exec(cmd, null, new File(projectPath));
        }

        if (AgentProcess != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(AgentProcess.getInputStream()));
            String str1 = null;
            while ((str1 = reader.readLine()) != null) {
                System.out.println(str1);
            }
        }

        if (AgentProcess != null && AgentProcess.waitFor() >= 0) {

            if(AgentProcess.exitValue() == 0) {
                status = true;
            }

        }

        AgentProcess.destroy();
        return status;
    }

}

所以请帮我解决如何实现此目标。它会失败并显示'/ bin / bash:mvn:命令未找到'

0 个答案:

没有答案