java jsp调用shell(带参数)脚本并返回值

test.jsp

<%@ page language="java" import="java.util.List,java.util.ArrayList,java.io.InputStreamReader,java.io.BufferedReader" pageEncoding="utf-8"%>
<%
 List<String> processList = new ArrayList<String>(); 
 String str="";
        try { 

  //11 aa为参数用空格隔开,也可以不带参数,与前面脚本用空格隔开,脚本带上绝对路径
         Process process = Runtime.getRuntime().exec("sh /usr/local/jboss5/server/default/deploy/sanguo_root.war/test.sh 11 aa");
         int exitValue = process.waitFor();
         //out.print(exitValue);脚本正确执行返回值为0
         if(0!=exitValue)
          process.destroy();
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); 
            String line = ""; 
            while ((line = input.readLine()) != null) { 
                processList.add(line); 
            } 
            input.close(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
 
        for (String line : processList) {
          str+=line;
          
        } 
        out.print(str+"");
%>

java技术交流群57388149

 

 

 

你可能感兴趣的:(shell)