无法在Cygwin中使用java运行Shell脚本

问题描述:

我正尝试使用java应用程序运行Shell脚本。我正在使用Process builder。无法在Cygwin中使用java运行Shell脚本

{ 
      String cmd; 
      cmd = "D:/cygwin/bin/bash -c '/bin/app.sh 121 121 1212 12121'"; 
      System.out.println("EXECING: " + cmd); 
      p = Runtime.getRuntime().exec(cmd); 

      in = p.getInputStream(); 
      br = new BufferedReader(new InputStreamReader(in)); 
      System.out.println("OUT:"); 
      while ((line = br.readLine()) != null) { 
       System.out.println(line); 
      } 

      in = p.getErrorStream(); 
      br = new BufferedReader(new InputStreamReader(in)); 
      System.out.println("ERR:"); 
      while ((line = br.readLine()) != null) { 
       System.out.println(line); 
      } 

      System.out.println(); 
     } 

此代码工程很好,当我使用简单的shell脚本像。

#!/bin/bash 
# Call this script with at least 3 parameters, for example 
# sh scriptname 1 2 3 4 
echo "first parameter is $1" 
echo "Second parameter is $2" 
echo "Third parameter is $3" 
echo "Third parameter is $4" 
exit 0 

任何一个可以建议我在哪里,我可以打开Cygwin和再参数shell脚本 becoz的方式。我的另一个Shell脚本在显示错误消息的相同位置不起作用。

app.sh: line 57: lib/renameapp.sh: No such file or directory 
app.sh: line 226: clear: command not found 
app.sh: line 69: grep: command not found 
app.sh: line 69: cut: command not found 
app.sh: line 74: grep: command not found 

任何一个可以建议我如何使用Java和Java运行shell脚本打开Cygwin的终端..提前

谢谢...

您应该设置您的%PATH环境变量适当地,或者在shell脚本中使用绝对路径。

+0

对不起,我忘了提及我已经安装我的PATH变量Cygwin \斌 –

PATH变量没有像你期望的那样被设置 - 当你从Java开始进程时,环境可能会丢失,或者Cygwin没有执行正常的PATH魔术,因为它不是登录外壳,我不确定。无论哪种方式,只需将export PATH="$PATH:/bin:/usr/bin:/usr/local/bin"添加到脚本的顶部,它几乎肯定会再次运行。