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)

Where did you placed this JAR file? Two options available TOMCAT_DIR/lib if you want library to be available globally to any project deployed on this server or TOMCAT_DIR/webapps/YOUR_PROJECT/WEB-INF/lib to be available to only your project

Where did you placed this JAR file? Two options available TOMCAT_DIR/lib if you want library to be available globally to any project deployed on this server or TOMCAT_DIR/webapps/YOUR_PROJECT/WEB-INF/lib to be available to only your project

Thank you...placed jar in tomcat\lib...working now..
but can u please explain that why tomcat also needs this jar, because I have already placed it the the project's build path.

I hope you are using only one location as there can be some issues if library placed in both "lib" directories

Alos not sure what you mean by "I have already placed it the the project's build path".

PS: There are specific steps you need to do for each IDE to link any library to the project

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.