无法从java运行shell脚本

时间:2012-06-28 06:59:41

标签: java bash shell unix

我有一个shell脚本,它有一个命令,用于在成功创建脚本后生成包含所需信息的文件。

现在,当我直接执行该命令或从cmd行执行脚本本身时,文件就会生成。

但是当我尝试使用Runtime.getRuntime().exec(cmd)从java代码执行相同的脚本时,文件没有生成。此行之前和之后的跟踪显示为预期表示脚本的执行不是抛出任何例外。

我哪里出错或者我错过了什么? Plz帮助!!!

示例这是我尝试执行的代码

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Scripttest {
public static void main(String args[])
{
        try{
String cmd="ls|grep sys";
        Process p=Runtime.getRuntime().exec(cmd);
        System.out.println("done");
        final BufferedReader input = new BufferedReader(new InputStreamReader( p.getInputStream()));
        String line = null;
        while ((line = input.readLine()) != null)
         System.out.println("proc: " + line);
}
        catch(Exception e)
        {
                System.out.println("Exception is"+e);
        }
}
}

和输出iam得到的是这个

java Scripttest
Exception isjava.io.IOException: Cannot run program "ls|grep": error=2, No such file or directory

cmd没有被执行但是不确定为什么不执行。

2 个答案:

答案 0 :(得分:2)

这是一个有点像使用管道的例子,在这个例子中脚本嵌入在java中。

import java.io.*;
class junk {
 public static void main (String args[]) {
  try {
   String line; String script;
   OutputStream stdin = null; InputStream stderr = null; InputStream stdout = null;
   Process p = Runtime.getRuntime ().exec ("/bin/bash");
   stdin = p.getOutputStream (); stderr = p.getErrorStream (); stdout = p.getInputStream ();
   script = "a=$(cat <<'@@@'\n" +
"ICAgICBfCiAgICB8IHwgX18gX19fICAgX19fXyBfICAgIF9fXyBfXyBfIF8gX18gICAgXyBfXyBfICAg" +
"XyBfIF9fCiBfICB8IHwvIF9gIFwgXCAvIC8gX2AgfCAgLyBfXy8gX2AgfCAnXyBcICB8ICdfX3wgfCB8" +
"IHwgJ18gXAp8IHxffCB8IChffCB8XCBWIC8gKF98IHwgfCAoX3wgKF98IHwgfCB8IHwgfCB8ICB8IHxf" +
"fCB8IHwgfCB8CiBcX19fLyBcX18sX3wgXF8vIFxfXyxffCAgXF9fX1xfXyxffF98IHxffCB8X3wgICBc" +
"X18sX3xffCB8X3wKICAgICAgICAgICAgICAgICAgICBfICAgICAgICAgICAgICAgXwogICAgICAgICAg" +
"ICAgICAgICAgfCB8X18gICBfXyBfIF9fX3wgfF9fCiAgICAgICAgICAgICAgICAgICB8ICdfIFwgLyBf" +
"YCAvIF9ffCAnXyBcCiAgICAgICAgICAgICAgICAgICB8IHxfKSB8IChffCBcX18gXCB8IHwgfAogICAg" +
"ICAgICAgICAgICAgICAgfF8uX18vIFxfXyxffF9fXy9ffCB8X3wK" +
"\n" +
"@@@)\n" +
"st=0\n" +
"for (( i=0; i<\"${#a}\"; i++ ))\n" +
"do\n" +
" x=${a:$i:1}\n" +
" in=$(($(expr index \\\n" +
" 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' \"\\\\$x\")-1))\n" +
" if [ $in -ge 0 ]; then case $st in\n" +
"  0 ) out=$(($in<<2)); st=3;;\n" +
"  1 ) out=$(($out|$in)); \n" +
"    printf \\\\$(printf '%03o' $(($out&255)) ) ; st=0 ;;\n" +
"  2 ) out=$(($out+($in>>2))); \n" +
"    printf \\\\$(printf '%03o' $(($out&255)) ) ;\n" +
"    st=0; out=$(($in<<6)); st=1;;\n" +
"  * ) out=$(($out+($in>>4))); \n" +
"    printf \\\\$(printf '%03o' $(($out&255)) ) ;\n" +
"    st=0; out=$(($in<<4)); st=2;;\n" +
"  esac fi\n" +
"done\n";
   stdin.write (script.getBytes ());
   stdin.close ();
   BufferedReader br = new BufferedReader (new InputStreamReader (stdout));
   while ((line = br.readLine ()) != null) { System.out.println(line); }
   br.close ();
   br = new BufferedReader (new InputStreamReader (stderr));
   while ((line = br.readLine ()) != null) { System.out.println ("2>" + line); }
   br.close ();
   p.waitFor ();
   System.out.println ("exit code " + p.exitValue ());
  }
  catch (IOException e) { e.printStackTrace (); }
  catch (java.lang.InterruptedException e) { e.printStackTrace (); }
 }
}

答案 1 :(得分:0)

将命令ls | grep sys保存在一个数组中,因为它包含两个主命令ls和grep。

最好的想法是在linux中使用shell脚本,在windows中使用批处理文件。根据您的要求使用命令创建shell脚本或批处理文件,然后从java代码执行shell脚本或批处理文件。