Tomcat 7 - Google Oauth2 - ClassNotFoundException - Java - Eclipse

时间:2015-12-16 21:10:48

标签: java eclipse tomcat

我正在尝试离线访问我的Google日历。 这个问题在此之前已得到解答: Offline Access to google calendar using java 但由于某种原因,我在尝试进行身份验证时总是会遇到ClassNotFoundException。

我在Apache Tomcat 7 Server上本地运行应用程序并使用Eclipse Mars。我通过Google API ManagerPlugin安装了以下API:

  • Google OAuth2 API v2
  • (Google OAuth API v1)该插件告诉我,v2是首选
  • Google Calendar API

通常我将外部jar文件复制到 WEB-INF / lib 文件夹和 Tomcat / lib 文件夹,但我想这个插件对我来说是对的吗?所有API都列在我的JavaBuildPath上。

如果我运行与 Java Application 相同的功能,那么它可以运行,因此Tomcat设置必定存在问题。

之前有没有人有过这样的问题,可以帮助我吗? 提前谢谢。

Servlet(已移除凭据):

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
    this.authenticate(request, response);
}


 public void authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        String client_id                = "";
        String redirect_uri             = "";
        String scope                    = "https://www.googleapis.com/auth/calendar";
        String client_secret            = "";
        List <String> scopes;
        HttpTransport transport         = new NetHttpTransport();
        JsonFactory jsonFactory         = new JacksonFactory();

        scopes = new LinkedList<String>();
        scopes.add(scope);
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build();
        GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl();
        url.setRedirectUri(redirect_uri);
        url.setApprovalPrompt("force");
        url.setAccessType("offline");
        String authorize_url = url.build();
        response.sendRedirect(authorize_url);
    }

控制台:

java.lang.ClassNotFoundException: com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1854)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
at net.meetme.CalendarServlet.authenticate(CalendarServlet.java:88)
at net.meetme.CalendarServlet.doGet(CalendarServlet.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
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:170)
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:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

感谢您的快速回复。 我将所有jar文件部署到我的WEB-INF / lib。

错误在于我还将所有jar文件添加到Java Build Path中。 您只需要从构建路径中删除这些jar文件即可。