Frage

I would like to test the twitter4j library, how could I use an arg in the main parameter, so that the program does not stop at the first parenthesis ?

public class MyTest {

    public static void main(String[] args) {
        if (args.length < 1) { //HERE, it is always < 1
            System.out.println("Usage: java twitter4j.examples.tweets.ShowStatus [status id]");
            System.exit(-1);
        }
        try {

EDIT :

the complete code (I replaced the keys with ... ) : I am actually wondering how I can ask twitter to show the details of my profile, since I only defined the keys of my app (no my account which is different).

public class MyTest {

    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("Usage: java twitter4j.examples.tweets.ShowStatus [status id]");
            System.exit(-1);
        }
        try {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true)
              .setOAuthConsumerKey("...")
              .setOAuthConsumerSecret("…")
              .setOAuthAccessToken("…")
              .setOAuthAccessTokenSecret("...");
            TwitterFactory tf = new TwitterFactory(cb.build());
            Twitter twitter = tf.getInstance();

            Status status = twitter.showStatus(Long.parseLong(args[0]));
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            System.exit(0);
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to show status: " + te.getMessage());
            System.exit(-1);
        }
    }
}
War es hilfreich?

Lösung

Well you could invoke MyTest with arguments. This would be dependent on your IDE and compiler, but in eclipse you can configure launch configurations (search Google for "eclipse launch configuration"). Or you could just just hack something in like so:

public class MyTest {

public static void main(String[] args) {

    args = { "something", "something else", "you get the idea" };

    if (args.length < 1) { //HERE, it is always < 1
        System.out.println("Usage: java twitter4j.examples.tweets.ShowStatus [status id]");
        System.exit(-1);
    }
    try {
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top