【问题标题】:Runtime.getRuntime().exec is not working for JSP in MAC machine/mac bookRuntime.getRuntime().exec 不适用于 MAC 机器/mac 书中的 JSP
【发布时间】:2014-09-15 11:50:03
【问题描述】:

我正在执行来自 jsp(例如:shell.jsp)的以下命令,并且我已经为 tomcat 服务器设置了。 我在 MAC 机器/mac book 中运行。

<%@ 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();

【问题讨论】:

  • 您是否导入了正确的Runtime 类?
  • 我已经导入了 java.lang.Runtime 类,但在 mac 机器/mac book 中仍然无法使用

标签: java shell jsp


【解决方案1】:

我有一个在 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();

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

【讨论】:

  • 我使用了 Process p = Runtime.getRuntime().exec("ls");但仍然是同样的问题..
  • 尝试调用 p.waitFor()。参考:docs.oracle.com/javase/7/docs/api/java/lang/Process.html
  • Runtime.getRuntime().exec()--> 在 mac 中不适合我,我浪费了 2 天。我找到了类似的课程,它对我有用。 try{ ProcessBuilder probuilder = new ProcessBuilder("/Users/admin/interface.sh");处理过程 = probuilder.start(); }catch (IOException e) { out.println(e.getMessage()); }
【解决方案2】:

<head>
<%   
    try{

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

        Process process = probuilder.start();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多