简体   繁体   English

Twitter4j流式API调用抛出类未找到异常

[英]Twitter4j streaming api call throwing class not found exception

I am writing a utility class using twitter4j for multiple purpose. 我正在使用twitter4j编写实用程序类,以实现多种用途。 I am able to search tweets based on Hashtags, location etc but Streaming API is not working for me. 我可以根据标签,位置等搜索推文,但Streaming API对我不起作用。

I have written a class with main method after following a blog as follows but I am getting class not found error.I am new to java. 我在跟随如下博客之后用main方法编写了一个类,但是我收到class not found error。我是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);
}

Error 错误

    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$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.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

My current hypothesis is that the problem is because of mixing core and stream jars of different versions (so eg TwitterStreamFactory from stream-3.0.3 expects HttpClientWrapperConfiguration to be available on your classpath but in 4.0.4 it is no longer included. Please try having those of the same version included (and only one version of lib, so stream-3 and stream-4 being included together is no-no). If that won't work - share the whole project somewhere for more context. 我目前的假设是问题是由于混合使用了不同版本的内核和流jar(例如stream-3.0.3的TwitterStreamFactory期望HttpClientWrapperConfiguration在您的类路径上可用,但在4.0.4中不再包含它。请尝试使用那些包含相同版本的文件(并且只有一个版本的lib,所以将stream-3和stream-4一起包含是不行的)。如果那行不通-在某个地方共享整个项目以获得更多上下文。

As for what classpath is you can google, or read up eg here What is a classpath? 至于什么类路径,你可以谷歌,或阅读例如这里是一个类路径?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM