我试图从jsp运行一个shell脚本,但我得到Ljava.lang.string错误

时间:2011-08-20 11:27:57

标签: java jsp shell

我有这样的代码:

<% Process p;
 String[] cmd =  "Z:\\walmart\\environment.sh";

 try {    
   p = Runtime.getRuntime().exec(cmd);
   StringBuffer s = new StringBuffer();

   BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream)));     

   while (input.readLine() != null)  
   {
     s.append(input.readLine() + "\n");      
   } 

   System.out.println(s.toString()); 

 }
 catch (IOException e) {  
    System.err.println("Failed to read & or start "); 
 }  

%>

2 个答案:

答案 0 :(得分:1)

String[] cmd = "Z:\walmart\environment.sh";

应该是

String[] cmd = {"Z:\walmart\environment.sh"};

这是一个字符串数组。不是char或字符串数​​组!

答案 1 :(得分:1)

1 - 我认为您需要转义文件路径中的反斜杠并将其设为数组:

String[] cmd = {"Z:\\walmart\\environment.sh"};

2 - 添加一般错误处理程序以找出错误:

catch (Throwable th)     {  
     System.err.println("Error:"); 
     th.printStackTrace();
  }