Nashorn in the Twitterverse

Jim Laskey
Senior Development Manager

I have been following how often Nashorn has been showing up on the net.  Nashorn got a burst of tweets when we announced Project Nashorn and I was curious how Nashorn was trending per day, maybe graph the result.  Counting tweets manually seemed mindless, so why not write a program to do the same.

This is where Nashorn + Java came shining through.  There is a very nice Java library out there called Twitter4J https://github.com/yusuke/twitter4j that handles all things Twitter.  After running bin/getAccessToken.sh to get a twitter4j.properties file with personal authorization, all I had to do to run my simple exploratory app was;

nashorn -cp $TWITTER4J/twitter4j-core-3.0.1.jar GetHomeTimeline.js

The content of GetHomeTimeline.js is as follows;

var twitter4j      = Packages.twitter4j;
var TwitterFactory = twitter4j.TwitterFactory;
var Query          = twitter4j.Query;

var twitter = new TwitterFactory().instance;
var query   = new Query("nashorn OR nashornjs");
query.count = 100;

do {
    var result = twitter.search(query);
    var tweets = result.tweets;

    for each (tweet in tweets) {
        print("@" + tweet.user.screenName + "\t" + tweet.text);
} while (query = result.nextQuery());

How easy was that?  Now to hook it up to the JavaFX graphing library... 

Join the discussion

Comments ( 2 )
  • guest Monday, May 20, 2013

    Hi Jim,

    I'm trying to use the MongoDB driver here but I can't instantiate the MongoClient class, do you have any idea why?

    var mongodb = Packages.com.mongodb;

    var MongoClient = mongodb.MongoClient;

    var mongoClient = new MongoClient();

    -- Running the test:

    jjs -cp mongo-java-driver-2-9-3.jar mongo.js

    -- result:

    mongo.js:4 TypeError: [JavaPackage com.mongodb.MongoClient] is not a function

  • jlaskey Monday, May 20, 2013

    The Mongo documentation is out of sync with the Java repo (moved to GitHub).

    Prior to 2.10, the name was Mongo, not MongoClient. You need to use the 2.10.x

    jar from https://github.com/mongodb/mongo-java-driver/downloads or


    var mongodb = Packages.com.mongodb;

    var MongoClient = mongodb.MongoClient;

    var mongo = new MongoClient("localhost");


    /Volumes/Elephant/Users/jlaskey/jjstest% jjs -cp mongo-2.10.1.jar mongodb.js

    Mongo: localhost/


    -- Jim

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.