gpt4 book ai didi

java - 线程 "main"java.lang.NoClassDefFoundError : com/google/common/base/Preconditions 中的 Google+ API 异常

转载 作者:行者123 更新时间:2023-11-29 10:06:14 26 4
gpt4 key购买 nike

我正在运行谷歌开发者网站上提供的 YouTubeSample。我的代码没有错误,我的导入似乎没问题。但是当我运行该项目时,出现了上述错误。

我进行了一些搜索,但老实说,我无法找出问题所在。我已经尝试导入外部 jar Guava ,但没有帮助。

感谢任何帮助。这是完整的类(class)

package com.pengilleys.googlesamples;
import java.io.IOException;
import java.util.List;

import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.googleapis.json.JsonCParser;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.util.Key;

public class YouTubeSample {

public static class VideoFeed {
@Key List<Video> items;
}

public static class Video {
@Key String title;
@Key String description;
@Key Player player;
}

public static class Player {
@Key("default") String defaultUrl;
}

public static class YouTubeUrl extends GenericUrl {
@Key final String alt = "jsonc";
@Key String author;
@Key("max-results") Integer maxResults;

YouTubeUrl(String url) {
super(url);
}
}

public static void main(String[] args) throws IOException {
// set up the HTTP request factory
HttpTransport transport = new NetHttpTransport();
final JsonFactory jsonFactory = new JacksonFactory();
HttpRequestFactory factory = transport.createRequestFactory(new HttpRequestInitializer() {

@Override
public void initialize(HttpRequest request) {
// set the parser
JsonCParser parser = new JsonCParser();
parser.jsonFactory = jsonFactory;
request.addParser(parser);
// set up the Google headers
GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName("Google-YouTubeSample/1.0");
headers.gdataVersion = "2";
request.headers = headers;
}
});
// build the YouTube URL
YouTubeUrl url = new YouTubeUrl("https://gdata.youtube.com/feeds/api/videos");
url.author = "searchstories";
url.maxResults = 2;
// build the HTTP GET request
HttpRequest request = factory.buildGetRequest(url);
// execute the request and the parse video feed
VideoFeed feed = request.execute().parseAs(VideoFeed.class);
for (Video video : feed.items) {
System.out.println();
System.out.println("Video title: " + video.title);
System.out.println("Description: " + video.description);
System.out.println("Play URL: " + video.player.defaultUrl);
}

} }

最佳答案

setup documentation给出依赖列表:

Depending on the application you are building, you may also need these dependencies:

在这种情况下,看起来缺少的是 Guava。我不知道您所说的“导出”Guava 是什么意思,但是如果您在运行代码时将 Guava r09 jar 文件包含在类路径中,应该没问题。

关于java - 线程 "main"java.lang.NoClassDefFoundError : com/google/common/base/Preconditions 中的 Google+ API 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7457668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com