首页 > 解决方案 > 从 java 程序运行 shell 脚本正在退出,代码为 255

问题描述

我在 cygwin 终端上从 java 程序运行简单的 shell 脚本,它运行良好,但是当我传递带有 2 个参数的不同 shell 脚本时返回退出代码 255。相同的 shell 脚本在没有 java 程序的情况下在 cygwin 终端上执行得很好。

在 cygwin 终端上使用的命令:/cygdrive/c/Users/MYNAME/Documents/abcScript.sh -u "username" -p "password123"

Java程序:

进程 proc = Runtime.getRuntime().exec(new String[] {"C:\cygwin64\bin\bash.exe", "-c", "cd /bin/ ; abcScript.sh -u '用户名' -p 'password123'" }, new String[] {"PATH=/cygdrive/c/cygwin64/bin"});

    StringBuilder output=new StringBuilder();
    BufferedReader reader=new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String line;
    while((line=reader.readLine())!=null){
        output.append(line+"\n");
    }
    int exitVal=proc.waitFor();
    System.out.println(exitVal);
    if(exitVal==0){
        System.out.println("Success");
        System.out.println(output);
        System.exit(0);
    }
    else{
        System.out.println("failed");
    }
    System.out.println(proc);


}

标签: javashellcygwin

解决方案


推荐阅读