Hello,
I'm not able to access jar files from servlet.
Although I have imported all the class file and made the jar file available in build path.
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
cmd = request.getParameter("cmd");
System.out.println(cmd);
String output = runCommand(cmd);
out.println(output);
}
public String runCommand(String cmd) {
System.out.println("cmd:"+cmd);
String output="";
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
JSch jsch = null;
Session session = null;
Channel channel = null;
ChannelSftp csftp = null;
String host = "snv8912";
String uname = "inro61";
String passwd = "Unix11!";
int port = 22;
try {
jsch = new JSch();
session = jsch.getSession(uname, host); // Creating Session
session.setConfig(props); // Building properties
session.setPassword(passwd);
session.setPort(port);
session.connect(); // Connecting via session
if (session.isConnected()) {
System.out.println(session.getServerVersion());
} else {
System.out.println("Not able to connect");
}
}catch (Exception e) {
e.printStackTrace();
}
return output;
}
I'm getting the following exception:
SEVERE: Servlet.service() for servlet ExecuteServlet threw exception
java.lang.NoClassDefFoundError: com/jcraft/jsch/JSch
at servlets.ExecuteServlet.runCommand(ExecuteServlet.java:64)
at servlets.ExecuteServlet.doPost(ExecuteServlet.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
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.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)