java.io.IOException: Cannot run program in Shell Scrip (Java Runtime.getRuntime().exec())


IOException while executing the unix shell command by java, Runtime.getRuntime().exec(), java.io.IOException, Cannot run program, No such file or directory.

JSP file where issue occurred:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.BufferedReader,java.io.IOException,java.*,java.lang.Runtime,java.io.InputStreamReader;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%
StringBuffer objOutPut = new StringBuffer();
Process objProcess;
try {
String[] command = {"ls -tlr"};

     objProcess = Runtime.getRuntime().exec(command);
   
BufferedReader reader = new BufferedReader(new InputStreamReader(
objProcess.getInputStream()));

String line = "";
while ((line = reader.readLine()) != null) {
System.out.println((line + "\n"));
objOutPut.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}

%>

<textarea rows="20" cols="150">
<%=objOutPut %> 
</textarea>
</body>
</html>

Issue:

java.io.IOException: Cannot run program "ls -tlr": java.io.IOException: error=2, No such file or directory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:494)
        at java.lang.Runtime.exec(Runtime.java:612)
        at java.lang.Runtime.exec(Runtime.java:485)
        at org.apache.jsp.jsp.cmd_005foutput_jsp._jspService(cmd_005foutput_jsp.java:75)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:701)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory

Solutions:

Put the command in .sh file and call that file with actual path.
e.g 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.BufferedReader,java.io.IOException,java.*,java.lang.Runtime,java.io.InputStreamReader;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%
StringBuffer objOutPut = new StringBuffer();
Process objProcess;
try {
String[] command = {"/home/catlog.sh"};
//String[] command = {"ls -tlr"};

     objProcess = Runtime.getRuntime().exec(command);
   
BufferedReader reader = new BufferedReader(new InputStreamReader(
objProcess.getInputStream()));

String line = "";
while ((line = reader.readLine()) != null) {
System.out.println((line + "\n"));
objOutPut.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}

%>

<textarea rows="20" cols="150">
<%=objOutPut %> 
</textarea>
</body>
</html>












Done for the day!!!

5 comments:

  1. I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. Feeling so glad about your article. Thanks…!
    best software testing training in chennai
    best software testing training institute in chennai with placement
    software testing training
    courses

    software testing training and placement
    software testing training online
    software testing class
    software testing classes in chennai
    best software testing courses in chennai
    automation testing courses in chennai

    ReplyDelete

Please provide your input, it will honer for me...