Twitter4j 流 api 调用抛出 class 未找到异常
Twitter4j streaming api call throwing class not found exception
我正在使用 twitter4j 编写一个实用程序 class 用于多种用途。我可以根据标签、位置等搜索推文,但流媒体 API 对我不起作用。
我在关注博客后用 main 方法编写了一个 class,但我发现 class 未找到 error.I 是 java 的新手。
package mytweetapp;
import twitter4j.FilterQuery;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
public class Stream {
public static void main(String[] args) throws TwitterException {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*****")
.setOAuthConsumerSecret(
"*******")
.setOAuthAccessToken(
"*****")
.setOAuthAccessTokenSecret(
"*****");
TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
TwitterStream twitter = tf.getInstance();
StatusListener listener = new StatusListener() {
public void onStatus(Status status) {
System.out
.println("@" + status.getUser().getScreenName() + " - " + status
.getText());
}
public void onDeletionNotice(
StatusDeletionNotice statusDeletionNotice) {
System.out
.println("Got a status deletion notice id:" + statusDeletionNotice
.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out
.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out
.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
public void onException(Exception ex) {
ex.printStackTrace();
}
@Override
public void onStallWarning(StallWarning arg0) {
// TODO Auto-generated method stub
}
};
FilterQuery fq = new FilterQuery();
String keywords[] = { "Mango", "Banana" };
fq.track(keywords);
twitter.addListener(listener);
twitter.filter(fq);
}
错误
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/internal/http/HttpClientWrapperConfiguration
at twitter4j.TwitterStreamFactory.<clinit>(TwitterStreamFactory.java:40)
at mytweetapp.Stream.main(Stream.java:23)
Caused by: java.lang.ClassNotFoundException: twitter4j.internal.http.HttpClientWrapperConfiguration
at java.net.URLClassLoader.run(URLClassLoader.java:372)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
我目前的假设是,问题是因为混合了不同版本的核心和流 jar(例如,来自 stream-3.0.3 的 TwitterStreamFactory 期望 HttpClientWrapperConfiguration 在您的类路径上可用,但在 4.0.4 中它不再可用包含。请尝试包含相同版本的那些(并且只有一个版本的 lib,因此 stream-3 和 stream-4 被包含在一起是不行的)。如果那行不通 - 在某个地方分享整个项目以获得更多上下文。
至于类路径是什么,您可以google,或者阅读例如这里 What is a classpath?
我正在使用 twitter4j 编写一个实用程序 class 用于多种用途。我可以根据标签、位置等搜索推文,但流媒体 API 对我不起作用。
我在关注博客后用 main 方法编写了一个 class,但我发现 class 未找到 error.I 是 java 的新手。
package mytweetapp;
import twitter4j.FilterQuery;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
public class Stream {
public static void main(String[] args) throws TwitterException {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*****")
.setOAuthConsumerSecret(
"*******")
.setOAuthAccessToken(
"*****")
.setOAuthAccessTokenSecret(
"*****");
TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
TwitterStream twitter = tf.getInstance();
StatusListener listener = new StatusListener() {
public void onStatus(Status status) {
System.out
.println("@" + status.getUser().getScreenName() + " - " + status
.getText());
}
public void onDeletionNotice(
StatusDeletionNotice statusDeletionNotice) {
System.out
.println("Got a status deletion notice id:" + statusDeletionNotice
.getStatusId());
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out
.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
public void onScrubGeo(long userId, long upToStatusId) {
System.out
.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
public void onException(Exception ex) {
ex.printStackTrace();
}
@Override
public void onStallWarning(StallWarning arg0) {
// TODO Auto-generated method stub
}
};
FilterQuery fq = new FilterQuery();
String keywords[] = { "Mango", "Banana" };
fq.track(keywords);
twitter.addListener(listener);
twitter.filter(fq);
}
错误
Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/internal/http/HttpClientWrapperConfiguration
at twitter4j.TwitterStreamFactory.<clinit>(TwitterStreamFactory.java:40)
at mytweetapp.Stream.main(Stream.java:23)
Caused by: java.lang.ClassNotFoundException: twitter4j.internal.http.HttpClientWrapperConfiguration
at java.net.URLClassLoader.run(URLClassLoader.java:372)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
我目前的假设是,问题是因为混合了不同版本的核心和流 jar(例如,来自 stream-3.0.3 的 TwitterStreamFactory 期望 HttpClientWrapperConfiguration 在您的类路径上可用,但在 4.0.4 中它不再可用包含。请尝试包含相同版本的那些(并且只有一个版本的 lib,因此 stream-3 和 stream-4 被包含在一起是不行的)。如果那行不通 - 在某个地方分享整个项目以获得更多上下文。
至于类路径是什么,您可以google,或者阅读例如这里 What is a classpath?