Twitter4j : Search for public tweets

You need to register your app for Twitter. You can only do that if you have your own private account. After you obtain ConsumerKey, ConsumerSecret, AccessToken and AccessTokenSecret (they will be presented to you afret you register your application), the simplest solution is to change the code to the following example:

public static void main(String[] args) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
          .setOAuthConsumerKey("yourConsumeKey")
          .setOAuthConsumerSecret("yourConsumerSecret")
          .setOAuthAccessToken("yourAccessToken")
          .setOAuthAccessTokenSecret("yourTokenSecret");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
        try {
            Query query = new Query("query");
            QueryResult result;
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
            }

            System.exit(0);
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to search tweets: " + te.getMessage());
            System.exit(-1);
        }
}

you need to register your application to get an OAuth Authentication Consumerkey, consumersecret and also Accesstoken and Accesstokensecret and provide these values to the configuration builder class.