Best java twitter library?

JavaTwitter

Java Problem Overview


The twitter API site lists 4 java twitter libraries.

Do you know others? What are your experiences in support, ease of use, stability, community, etc.

Java Solutions


Solution 1 - Java

I just took a look at them.

JTwitter definitely looks like the weakest of the three. It doesn't appear to have been updated lately, doesn't cover much of the Twitter API, and doesn't appear to have any releases besides the initial one. On the upside, it's LPGL licensed, comes packaged with what little extra code it needs, and looks small and simple to understand.

The other two, java-twitter and Twtter4J look much closer in quality. Both cover the core API, and both have all the accouterments of normal open-source projects: publicly available source code repository, on-line docs, active mailing lists, and recent development activity.

However, Twitter4J looks to be the leader. From the docs, its API coverage appears to be more complete. The mailing list is definitely more active. The docs are much better. And most importantly to me, the releases have been more frequent. java-twitter has one release up, the "0.9-SNAPSHOT" release about 4 months ago. Twitter4J has had several releases in that time period, including 2.0.0 and incremental releases up through 2.0.8, fixing issues and adding support for new Twitter APIs.

I'm going to start with Twitter4J; if you don't hear back, assume it was just great for me.

Solution 2 - Java

I think Twitter4j is good one it is most upto date API

Solution 3 - Java

Ahem; JTwitter is actively maintained, regularly updated (version 1.6.2 released today), and covers most of the Twitter API.

It's missing only a method for setting your profile image and a few other profile settings. Other than that, it's pretty complete. Status updates, timelines, friendships, lists, searches, streaming, etc.

It's also fast and robust. Twitter can be flaky in places and JTwitter has work-arounds and informative exceptions to make your life easier.

As the main JTwitter developer, I am rather biased! But the feedback from developers using JTwitter is also very positive.

Solution 4 - Java

I use Twitter4J and have yet to have a problem with it. I actually like it a lot.

The OAuth example they give on their website is the biggest nuisance -- it's not helpful. Here's my OAuthServlet code if you're interested (or anyone else). I know this question is rather old, so I'm putting it in here more for search results.

package com.example.oauth;

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import twitter4j.*;
import twitter4j.http.*;


@SuppressWarnings("serial")
public class OAuthServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

	    HttpSession sess = req.getSession(true);
    	RequestToken requestToken = (RequestToken) sess.getAttribute("requestToken");

    		

		if (sess.getAttribute("twitter_user_id") != null) {
			resp.setContentType("text/plain");
			PrintWriter out = resp.getWriter();
			if (req.getRequestURI().indexOf("logout") > 3) {
	    		sess.removeAttribute("twitter_user_id");
	    		out.println("You're now logged out.");
	    	} else {
	    		out.println("You're already logged in!");
	    	}
    	} else if (req.getRequestURI().indexOf("callback") > 3 && req.getParameter("oauth_token").length() > 0 && requestToken != null) {

    		handleCallback(req, resp, sess);
    		if (sess.getAttribute("oauth_previous") != null) {
    			resp.sendRedirect((String) sess.getAttribute("oauth_previous"));
    			sess.removeAttribute("oauth_previous");
    		}
    		
		} else {
			
			sendToTwitter(resp, sess);
			sess.setAttribute("oauth_previous", req.getHeader("Referer"));
		}

	}
	
	private void sendToTwitter(HttpServletResponse resp, HttpSession sess) throws IOException {

    	RequestToken requestToken = (RequestToken) sess.getAttribute("requestToken");

    	try {
		    Twitter twitter = new TwitterCnx().registerOAuth().get();
			
			requestToken = twitter.getOAuthRequestToken();
			sess.setAttribute("requestToken", requestToken);
			
		    resp.sendRedirect(requestToken.getAuthorizationURL());
		    
		    
		} catch (TwitterException e) {
			PrintWriter out = resp.getWriter();
			out.println(e.getStackTrace());
		}
	}
	
	private void handleCallback(HttpServletRequest req, HttpServletResponse resp, HttpSession sess) throws IOException {
    	
		RequestToken requestToken = (RequestToken) sess.getAttribute("requestToken");
		sess.removeAttribute("requestToken");
		String secret = req.getParameter("oauth_token");
		
		resp.setContentType("text/plain");
		PrintWriter out = resp.getWriter();
		
	    try {
			Twitter t = new TwitterCnx().registerOAuth().get();
	    	
			AccessToken accessToken = t.getOAuthAccessToken(requestToken.getToken(), secret);
	    	long id = (long) t.verifyCredentials().getId();
			
			storeAccessToken(id, accessToken,sess);
			sess.setAttribute("twitter_user_id", id);
			
			out.println("You're now logged in!");
			
		} catch (TwitterException e) {
			out.println("Something went wrong. Sorry about that. Please try again.");
		}

	}
	
	
	
	private void storeAccessToken(Long id, AccessToken at, HttpSession sess) {
		//you probably want to persist this somewhere other than in sessions.
		sess.setAttribute("secret", at.getTokenSecret() );
		sess.setAttribute("token", at.getToken());
		//also, don't forget to persist the user id alongside the token.
	}
}

I use the class TwitterCnx as a wrapper for twitter4j.Twitter. TwitterCnx defines my OAuth consumer stuff and returns a Twitter object. It's a final class with static methods so I don't produce more than one Twitter object per app instance.

Solution 5 - Java

For anyone still looking at this question, it's worth mentioning that the only API that Twitter currently lists on its website for Java is Twitter4J. So, if you needed any more convincing that this was the one to use, well, here you go!

Solution 6 - Java

I have selected Twitter4j because a lot of people are working on it. And its easy to find out some contents on Internet about it.

Solution 7 - Java

There's also TweetStream4J which is a Java binding for the Twitter Streaming API. It's pretty simple, and unlike the last time I used it, the author has updated it to include a pom.xml file so you can build it. It's pretty simple and quick when I last used it (from Scala).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionflybywireView Question on Stackoverflow
Solution 1 - JavaWilliam PietriView Answer on Stackoverflow
Solution 2 - JavaRahul GargView Answer on Stackoverflow
Solution 3 - JavaDaniel WintersteinView Answer on Stackoverflow
Solution 4 - JavaTed PenningsView Answer on Stackoverflow
Solution 5 - JavaAbsentMonikerView Answer on Stackoverflow
Solution 6 - JavaTahirView Answer on Stackoverflow
Solution 7 - JavaTom MorrisView Answer on Stackoverflow