1

I have created simple web application in jsp/servlet.

After login, want to establish socket communication over TCP/IP to remote server [IP and port is available]

I have created jar file of the class which is doing socket communication.

When I use the jar file in servlet, I get below exception

Feb 10, 2016 3:28:40 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.Temp.ServerServlet] in context with path [/Temp_Hosting] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoClassDefFoundError: TCPClient/TCPClient
 at com.temp.ServerServlet.doPost(ServerServlet.java:51)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
 at java.lang.Thread.run(Unknown Source)

However, When I use the same class file from which jar is been build, it works perfectly fine.

So in servlet, calling below method of class which is doing socket communication works fine.

Below is the method definition is class

 public String initializeServer(String _ipAddress, int portNo){
        String msg = "Failure";
        BufferedReader br = null;
        BufferedOutputStream bos = null;
        byte buffer[] = null;
        String send;
        try {
               Socket socket = new Socket(_ipAddress, portNo);
               br  = new BufferedReader(
                            new InputStreamReader(socket.getInputStream(), "UTF-8"));
               bos  = new BufferedOutputStream(socket.getOutputStream());
               send = "hi server\r\n";
               buffer = send.getBytes("UTF-8");
               bos.write(buffer,0,buffer.length);
               bos.flush();
               msg = br.readLine();
               System.out.println("Msg is :"+msg);
               bos.close();
               br.close();
               socket.close();
        } catch (UnknownHostException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
        } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
        }
        return msg;
        
 }

Note:

  1. jar file is copied to WEB-INF/class folder and even added to classpath also but still no result.
  2. The same jar file is working correctly in normal java application.

Thanks in advance for help..!!

School Boy
  • 1,207
  • 2
  • 19
  • 33
  • You shall never copy jar files into WEB-INF/class (or WEB-INF/classes, which is the correct name for the folder). Copy them into WEB-INF/lib. – Michal Feb 10 '16 at 10:23
  • @Michal: Thanks for the feedback. Sorry about typo, thats classes and I tried with WEB-INF/lib also which is not working. – School Boy Feb 10 '16 at 10:26
  • Could you upload the jar file? It is neccessary that the jar file has following structure: folder called TCPClient and in this folder a file called TCPClient.class. – Michal Feb 10 '16 at 10:30
  • Second, how do you start the application? From Eclipse? From cmd line? What are deploying into...Tomcat? – Michal Feb 10 '16 at 10:31
  • Yes Eclipse/Tomcate. – School Boy Feb 10 '16 at 10:36
  • You need to verify that the war deployed by eclipse contains the aformentioned jar in WEB-INF/lib (when Eclipse starts Tomcat it logs where it is deploying the war file) and that the jar has the correct structure (see my comment above). Regarding the first condition you might also want to check Eclipse Deployment Assembly, available in Project Properties. – Michal Feb 10 '16 at 10:40

1 Answers1

1

Put the jar into WEB-INF/lib as instructed by Oracle.

Michal
  • 2,353
  • 1
  • 15
  • 18