Live Tweets using Streaming API


  • Use twitter streaming API to fetch live twitter streams

  • The streams will be displayed to the Console.

  • You can also retain the tweets by adding a log out to a log file using

  • You can specify the search string for twitter.

Step 1 : Sign up for a twitter account at If you already have access keys and tokens move to step 4.

Step 2. Create an app in All you need to enter are name (globally unique), description, a url. You can change the url later.

Step 3: Now you need to obtain 2 sets of details - keys and tokens. Both are available on the same page. Click on "Generate My Access Token" to generate tokens.

Make sure you got these following details

  • Consumer Key (API Key)

  • Consumer Secret (API Secret)

  • Access Token

  • Access Token Secret

Use the above values in the file as describe in the steps below.

Step 4: Create a java maven project in your IDE of choice. Use the following POM details.




<name> Repository</name>






















Step 5: Add a new java class "".

import twitter4j.*;

public final class Stream {

private static final Logger logger = Logger.getLogger(Stream.class);


* Main entry of this application.


* @param args arguments doesn't take effect with this example

* @throws TwitterException when Twitter service or network is unavailable


public static void main(String[] args) throws TwitterException {

TwitterStream twitterStream = new TwitterStreamFactory().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 onStallWarning(StallWarning warning) {

System.out.println("Got stall warning:" + warning);


public void onException(Exception ex) {





if(args.length == 1){







Step 6: Create a new directory "resources" inside your project and right click > Build Path > Use As Source Folder.

Step 7: Inside resources directory, create a new file Fill in the place holders with details from the step 3.



oauth.consumerKey=<enter your details>

oauth.consumerSecret=<enter your details>

oauth.accessToken=<enter your details>

oauth.accessTokenSecret=<enter your details>

Step 8: Now you can export this project as jar and run it. Make sure your working directory (... that contains the project jar) contains the maven dependency jar.

$ java -cp "*" Stream "trump, hillary"

Step 9: If you want to retain the tweets, add a log4j property using to your project. Following is an example:




log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n







log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %m%n