Skip to main content

Running Twitter4J on phoneMe Advanced

Posted by brunogh on November 22, 2010 at 8:46 AM PST

Today we are gonna talk about how to create a Java application for Digital TV that integrates with Twitter. Note that the demo will be based on the basic Java DTV stack, without any specific middleware definitions, so feel free to add UI later.

phoneMe Advanced is the basic stack (CDC/FP/PBP) for DTV middlewares. I started looking at a Twitter library in Java and found an interesting one called Twitter4J ( Twitter4J has a target compilation for Java 1.4, which is needed, because phoneME Advanced is a subset of Java 1.4 (does not have some packages and methods) compatible with the language syntax. Most of the others libraries were only for 1.5.

  1. Download phoneME Advanced ( and compile it (
  2. Create a new Java Project and add basis.jar and in the classpath (it will be generated after compiling phoneMe Advanced). Also remove the default JRE at Java Build Path->Libraries, if you are using Eclipse.
  3. Download Twitter4J ( and add it to the project classpath.

Create a class like above. Xlet is an interface that is needed to be implement, so AMS (Application Management System) can run it in a sandbox.

import java.util.ArrayList;
import java.util.List;
import javax.microedition.xlet.Xlet;
import javax.microedition.xlet.XletContext;
import javax.microedition.xlet.XletStateChangeException;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import com.sun.xlet.XletContextImpl;

public class TwitterXlet implements Xlet {

    protected XletContextImpl context;

    public void initXlet(XletContext xletContext)
            throws XletStateChangeException {
        context = (XletContextImpl) xletContext;

    public void destroyXlet(boolean unconditional)
            throws XletStateChangeException {

    public void pauseXlet() {

    public void startXlet() throws XletStateChangeException {
        List result ="#java");
        for (int i = 0; i < result.size(); i++) {
            Tweet tweet = (Tweet) result.get(i);
            System.out.println(tweet.getFromUser() + " " + tweet.getText());

    public List search(String s) {
        List toReturn = new ArrayList();
        try {
            Twitter twitter = new TwitterFactory().getInstance();
            Query query = new Query(s);
            QueryResult result;
            result =;
            toReturn = result.getTweets();
        } catch (TwitterException e) {
        return toReturn;

Create a twitter.policy file that will be passed to override on execution for test purposes:
grant {

Now, note that you will need to add xerces.jar ( into the execution classpath when invokes the XletRunner to run the TwitterXlet. This is because Twitter4J depends on org.xml.sax package and the stack does not provide implementation.

To run the Xlet application:
${PHONEME_PATH}/cdc/build/linux-x86-generic/bin/cvm${POLICY_PATH}/twitter.policy com.sun.xlet.XletRunner -name TwitterXlet -path

Output (a lot fo people from all around the world is quoting Java!):
@@XletRunner starting Xlet TwitterXlet
[Mon Nov 22 12:59:10 GMT-03:00 2010]Will use class twitter4j.internal.logging.StdOutLoggerFactory as logging factory.
[Mon Nov 22 12:59:10 GMT-03:00 2010]Will use twitter4j.internal.http.HttpClientImpl as HttpClient implementation.
darksage07 Alguem me pode explicar ond é que ta o erro neste sniippet k fiz em #java ?
baselogic #java #javaee I can agree with everything except for "Python 2.5 is old". Its not old, its stable for product... pt
baselogic #java #javaee Reconfiguring mac for heavy JBoss, J2EE, work. #mac #java# RT @bitsmonkey: pt
baselogic #java #javaee Configuration Management Database - You can move forward even if you don't want a complete CMDB... pt
baselogic #java #javaee Forgot url for mobile dev with spring RT @vdandre: pt
baselogic #java #javaee FREE Red Hat Webinar - Insight into Performance Improvements JBoss Enterprise Platforms- Nov 23... pt
top_java_jobs Myspace Feed System Replica by creativefuse: I am looking for a feed system similar to the New Myspa... #Java #Jobs
ruglax pregunta de novato, en #java, para una aplicacion #JavaSE, cual es la forma correcta de ,anejar usuarios?
enterprisejava #java #javaee Will the "NoSQL" fad thrive or end?# RT @riyaaz: pt /via @baselogic:
enterprisejava #java #javaee JBoss Application Server 7 looks shockingly good (Asylum #14 podcast) - RT......
baselogic #java ???????? iOS 4.2 ?????? ???????: ???????? Apple ????????? ?????-?????, ? ??????? ... jb
baselogic #java Google datastore discussion: The Disco BlogGoogle’s datastore, known as Bigtable, is a column-oriented ... jb
baselogic #java ??? ??????????? iOS 4.2 ??? iPhone/iPad ? 15-???????? ?????: ?? ??? ???? ?????? ????... jb
baselogic #java 2010? 11? 22?, ????: ???? ?? ??? [??™]2010-11-22 16:00:21 ???. ^^ infinitest…  ?? ~ ????~!! by ... jb
baselogic #java Agile Testing UK – plans for the future: Gojko AdzicAt the BDD and Agile Testing exchange conference on... jb

ps: Thanks to Yusuke Yamamoto for the support in the users list. Great project!

Have fun!
Bruno Ghisi


Related Topics >>