0

I am writing a utility class using twitter4j for multiple purpose. I am able to search tweets based on Hashtags, location etc but Streaming API is not working for me.

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.

    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
2FaceMan
  • 443
  • 2
  • 18
  • 34
  • are you running this from inside of IDE/mvn cli? can you please upload the whole project to github so it will be possible to answer context questions (e.g. which version do you have, what's your classpath, ...) – Ivan Sep 23 '16 at 11:16
  • @Ivan I am running this class on eclipse by calling the main method twitter4j-core-4.0.4.jar twitter4j-stream-3.0.3.jar are the jar versions also what class path you are you asking about is it for this class?I am new to all this – 2FaceMan Sep 23 '16 at 12:05
  • you can upload the whole folder with .jar, .project, .classpath, .java and other files to e.g. github to make it easier for others to debug. – Ivan Sep 24 '16 at 17:38
  • @Ivan I have uploaded the project on gDrive as I was unable to put on github . https://drive.google.com/file/d/0B0gxtpfo7Zs1dnk1QmZiZTUwY28/view?usp=sharing – 2FaceMan Sep 26 '16 at 08:50
  • can you try using core and stream jars of the same versions and report if that worked for you and if not upload project with that? – Ivan Sep 26 '16 at 15:01

1 Answers1

3

My current hypothesis is that the problem is because of mixing core and stream jars of different versions (so e.g. 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.

As for what classpath is you can google, or read up e.g. here What is a classpath?

Community
  • 1
  • 1
Ivan
  • 3,781
  • 16
  • 20
  • I have uploaded the project on gDrive as I was unable to put on github . https://drive.google.com/file/d/0B0gxtpfo7Zs1dnk1QmZiZTUwY28/view?usp=sharing – 2FaceMan Sep 26 '16 at 08:53
  • 1
    you are right , jar version was the issue . Thanks :) – 2FaceMan Sep 26 '16 at 15:46