运行Calendar Api示例代码时出错

时间:2015-12-08 11:41:38

标签: java google-calendar-api

尝试实施从https://developers.google.com/google-apps/calendar/quickstart/java获取的Google日历代码,但收到如下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/mortbay/jetty/Handler
at CalendarQuickstart.authorize(CalendarQuickstart.java:76)
at CalendarQuickstart.getCalendarService(CalendarQuickstart.java:89)
at CalendarQuickstart.main(CalendarQuickstart.java:101)
Caused by: java.lang.ClassNotFoundException: org.mortbay.jetty.Handler
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

这条线是

 Credential credential = new AuthorizationCodeInstalledApp(
        flow, new LocalServerReceiver()).authorize("user");

在此之前我只是从https://developers.google.com/google-apps/calendar/quickstart/java复制了代码并创建了一个Java项目并且缺少这两个Jar文件

import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;

导入这些jar文件后得到了这个错误。请帮忙

1 个答案:

答案 0 :(得分:0)

Google Calendar API Java Quickstart工作正常。根据您收到的错误消息,您似乎错过了其中一个jetty依赖项。如果您正在使用Gradle版本(根据here中的建议,请查看您的" Gradle Depenencies"对于此jar文件 - jetty-6.1.26.jar

如果您错过了码头依赖关系,请打开build.gradle并检查以确保包含下面列出的所有依赖关系。

dependencies {
     compile 'com.google.api-client:google-api-client:1.20.0'
     compile 'com.google.oauth-client:google-oauth-client-jetty:1.20.0'
     compile 'com.google.apis:google-api-services-calendar:v3-rev125-1.20.0'
}

最后,构建并运行Gradle文件以下载缺少的依赖项。你现在应该开始运行了! :)

祝你好运!