2013-02-26 95 views
1

即時通訊使用Eclipse IDE和當我運行我的程序一切工作正常,但是當我在服務器上運行相同的程序,然後它給了我這個例外,我已經把我所有的遠程文件以及在我的lib文件夾,請幫我簡單解釋一下這個問題。例外在Apache HTTP客戶端

java.lang.ClassNotFoundException: org.apache.http.HttpRequest 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701) 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546) 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2836) 
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1160) 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1668) 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546) 
    at org.controller.KeyRank.doPost(KeyRank.java:41) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:722)

這裏是我的代碼:

 public String readGoogle(String keyword, int page) { 
     HttpClient client = new DefaultHttpClient(); 

     // Create a method instance 
     keyword = keyword.trim(); 
     keyword = keyword.replaceAll(" ", "+"); 

     String url = "http://www.google.com/search?q="+keyword+"&start="+page+"&btnG=Google+Search&aq=f&oq="; 
     String responseBody = ""; 
     HttpGet method = new HttpGet(url); 

     //Provide custom retry handler is necessary 
     //:OC:method.getParams().setParameter(HttpConnectionParams.RETRY_HANDLER, new DefaultHttpRequestRetryHandler(3, false)); 


     try{ 
     //execute the method 


      System.out.println("executing request " + method.getURI()); 

      // Create a response handler 
      ResponseHandler responseHandler = new BasicResponseHandler(); 
      responseBody = client.execute(method, responseHandler); 
      System.out.println("----------------------------------------"); 
      System.out.println(responseBody); 
      System.out.println("----------------------------------------"); 



      } catch (HTTPException e) { 

        System.err.println("Fatal protocol violation: " + e.getMessage()); 
        e.printStackTrace(); 

       } catch (IOException e) { 

        System.err.println("Fatal transport error: " + e.getMessage()); 
        e.printStackTrace(); 
       } finally { 

        //release the connection 
       method.releaseConnection(); 

     } 
     return responseBody; 

    }
+1

ClassNotFoundException只能表示指定的類不在您的類路徑中。再次檢查所有正確的罐子是否在你的類路徑上。 – 2013-02-26 19:00:37

+0

是的,我已經添加了所有的罐子以及我已經添加jar到我的WEB-INF/lib文件夾(httpclient-4.2.3.jar),但它給我例外相同的代碼工作時,我運行它使用主。但不工作時,我在本地主機上運行它 – ashishchaudharyjavaoracle 2013-02-26 19:03:15

+0

@ashishchaudharyjavaoracle你需要httpcore jar文件。在下面檢查我的答案。 – 2013-02-26 19:09:31

回答

1

沒有你classpath configuration和知識,你將應用程序部署到Web服務器,這是不容易的幫助,但是從事物的樣子,似乎您在部署環境中缺少HttpCore jar。

在該jar中找到缺少的類。

2

將Http核心jar添加到您的構建路徑中,從此url中查找所需的jar。 http://www.java2s.com/Code/Jar/h/Downloadhttpcorejar.htm

部署該jar然後編譯,你的錯誤將被解決。

+0

雖然這可能在理論上回答這個問題,[這將是更可取的](/ meta.stackoverflow.com/q/8259)在這裏包括答案的基本部分,並提供參考鏈接。 – 2015-09-09 07:20:45

+0

實際上爲我工作.. – prabhugs 2016-03-03 10:00:47