简体   繁体   English

无法通过 java 程序在 mac os 中运行 shell 脚本

[英]unable to run shell script in mac os thru java program

I'm trying to run shell script thru Java in Mac OS.我正在尝试在 Mac OS 中通过 Java 运行 shell 脚本。 The shell script has git clone command clone a repository. shell 脚本具有 git clone 命令克隆存储库。

I have tried using process builder API.我尝试过使用流程构建器 API。 It's not giving any exception though but the repo is not cloning when I run the code.虽然它没有给出任何异常,但是当我运行代码时,repo 没有克隆。

public class Test {
public static void main(String[] args) throws IOException  {

    Process p;
    try {

        List<String> cmdList = new ArrayList<String>();

        cmdList.add("/Users/Folder/AnotherFolder/Another/Final/clone.sh");
        ProcessBuilder pb = new ProcessBuilder(cmdList);
        p = pb.start();

        p.waitFor(); 
        BufferedReader reader=new BufferedReader(new InputStreamReader(
                p.getInputStream())); 
        String line; 
        while((line = reader.readLine()) != null) { 
            System.out.println(line);
        } 
    } catch (Exception e) {

        e.printStackTrace();
        System.out.println(e.getMessage());
    } 
}

}

Expecting to clone git project in the path, but not giving any output or Exception.期望在路径中克隆 git 项目,但未给出任何 output 或异常。

Above JAVA code works fine, I suspect there could be some issue with the script.Because git local repo will not be know when java program tries to execute the shell script. Above JAVA code works fine, I suspect there could be some issue with the script.Because git local repo will not be know when java program tries to execute the shell script.

On executing program with below script git clone works fine.使用以下脚本执行程序 git 克隆工作正常。

#!/bin/bash
mkdir ~/repo
cd ~/repo
git init
#git config user.email "email"
#git config user.name "user"
/usr/local/bin/git clone https://github.com/divaibhav/helloworld

You are ignoring any error messages emitted by your script.您忽略了脚本发出的任何错误消息。

Remove all use of p.getInputStream() , and replace it with a call to inheritIO() :删除p.getInputStream()的所有使用,并将其替换为对inheritIO()的调用:

try {
    ProcessBuilder pb = new ProcessBuilder(
        "/Users/Folder/AnotherFolder/Another/Final/clone.sh");

    pb.inheritIO();

    p = pb.start();
    p.waitFor();
} catch (IOException | InterruptedException) {
    e.printStackTrace();
}

When you were calling p.getInputStream() , you were only reading the standard output of the process.当您调用p.getInputStream()时,您只是在阅读该过程的标准 output。 inheritIO() will cause both the standard output and the standard error of the child process to appear in the Java process's own standard output and standard error. inheritIO()会导致标准 output 和子进程的标准错误同时出现在 Java 进程自己的标准 output 和标准错误中。 This will allow you to see all diagnostic messages printed by the script.这将允许您查看脚本打印的所有诊断消息。 In particular, error messages usually appear on standard error, not standard output.特别是,错误消息通常出现在标准错误上,而不是标准 output。

This is copied from one of my libraries.这是从我的一个图书馆复制的。 I use it every day.我每天都使用它。 I hope I am not troubling you, but I am on a cell, looking at the Q&A.我希望我没有打扰你,但我在一个牢房里,看着问答。 This works great with executables - I am not 100% regarding '.sh' files...这对可执行文件很有效——我不是 100% 关心 '.sh' 文件......

Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader[] ret = new BufferedReader[2];
ret[0] = new BufferedReader(new InputStreamReaderr(proc.getInputStream()));
ret[1] = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
return ret;

One point of order - will Mac OS run UNIX Shell scripts, in the first place?一个程序点 - 首先,Mac OS 会运行 UNIX Shell 脚本吗? Do you have some kind of emulator running?你有某种模拟器在运行吗? Are you connecting to a cloud development server?您是否正在连接到云开发服务器? I connect through the cloud to GCS/GCP - so, technically, I am never on a Windows or Mac (just locally).我通过云连接到 GCS/GCP - 所以,从技术上讲,我从来没有在 Windows 或 Mac 上(只是在本地)。

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

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