Runtime.getRuntime()。exec不适用于MAC机器/ mac书中的JSP

时间:2014-09-15 11:50:03

标签: java shell jsp

我正在执行jsp下面的命令(例如:shell.jsp),我已经设置了tomcat服务器。 我在MAC机器/ mac书中运行。

<%@ page import="java.util.*,java.io.*" %>
<%@ page import="java.lang.Runtime" %>

String unixCommand = "sh /Users/admin/interface.sh";
Runtime rt = Runtime.getRuntime();
rt.exec(unixCommand);

我收到以下错误

An error occurred at line: 9 in the jsp file: /file_upload_download/shell.jsp
**The method exec(String) is undefined for the type Runtime**
   String unixCommand = "sh /Users/admin/interface.sh";
      Runtime rt = Runtime.getRuntime();

2 个答案:

答案 0 :(得分:0)

我在Windows / MAC上有一个使用公共代码的应用程序。以下是我如何使用它:

Process p = Runtime.getRuntime().exec(<your command>);
InputStream stderr = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line);
int exitCode = p.waitFor();

流读取有助于检查执行的消息。

答案 1 :(得分:0)

<head>
<%   
    try{

ProcessBuilder probuilder = new ProcessBuilder(&#34; /Users/admin/interface.sh");

        Process process = probuilder.start();

        }catch (IOException e) {
            out.println(e.getMessage());
        }        
%>
</head>
<body>