0

I used the following code to collect tweets. When compile there is no any error. But when I compile this program it shows the exceptions.

package com.crowley.simplestream;

import twitter4j.FilterQuery;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.StallWarning;

public class SimpleStream {
    public static void main(String[] args) {
        ConfigurationBuilder cb = new ConfigurationBuilder();

        cb.setDebugEnabled(true);

        cb.setOAuthConsumerKey("*********************");
        cb.setOAuthConsumerSecret("*******************");
        cb.setOAuthAccessToken("********************");
        cb.setOAuthAccessTokenSecret("*********************");

        TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

        StatusListener listener = new StatusListener() {
            @Override
            public void onException(Exception arg0) {
                // TODO Auto-generated method stub

            }
            @Override
            public void onDeletionNotice(StatusDeletionNotice arg0) {
                // TODO Auto-generated method stub

            }
            @Override
            public void onScrubGeo(long arg0, long arg1) {
                // TODO Auto-generated method stub

            }
            @Override
            public void onStatus(Status status) {
                User user = status.getUser();

                // gets Username
                String username = status.getUser().getScreenName();
                System.out.println(username);
                String profileLocation = user.getLocation();
                System.out.println(profileLocation);
                long tweetId = status.getId(); 
                System.out.println(tweetId);
                String content = status.getText();
                System.out.println(content +"\n");

            }

            @Override
            public void onTrackLimitationNotice(int arg0) {
                // TODO Auto-generated method stub

            }
           @Override
           public void onStallWarning(StallWarning warning){
           }

        };
        FilterQuery fq = new FilterQuery();

        String keywords[] = {"ireland"};

        fq.track(keywords);

        twitterStream.addListener(listener);
        twitterStream.filter(fq);  

    }
}

The exception which I am getting when I run this program is as follows.

Exception in thread "main" java.lang.NoClassDefFoundError: SimpleStream (wrong name: com/crowley/simplestream/SimpleStream)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)

I used Jwitter4j. But it does not contains the packages like ClassLoader.java. What can I do? Please help me.

David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • (I'm leaving my answer here for now since it may be more helpful to the asker; if the question is closed, I'll delete it.) – David Conrad Jun 20 '14 at 19:01

1 Answers1

1

How is your project set up? Are you compiling and running this from the command line or in an IDE? When you have a class named SimpleStream with a package com.crowley.simplestream; statement at the top, then com.crowley.simplestream.SimpleStream is the fully qualified name of the class. The javac compiler will emit the .class file in the current directory by default, but it expects the class to be found in com/crowley/simplestream/SimpleStream when you run it.

Normally you would keep the source for com.crowley.simplestream.SimpleStream in a file com/crowley/simplestream/SimpleStream.java in your project, typically underneath a src or src/main/java directory, and your IDE (such as Eclipse or IDEA) would build all the source files in that directory into a parallel output directory structure with all the .class files. It sounds like you may be building everything yourself from the command line, which of course you can do, but you must take into account the fully qualified name of the class and where Java then expects to find it.

For example, you could put the source into an appropriate directory and type the full path to it when invoking javac, or you could use the -d switch to javac to tell it where to put the .class file, and you would then use the fully qualified name of the class when running it:

java com.crowley.simplestream.SimpleStream

David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • Sir,Thanks for your explanation. I run my program using command line. – user3759778 Jun 21 '14 at 05:40
  • Now I changed my package name into **package simplestream**. The location of my java file is **E:\program files\JAVA\net\simplestream**. Then I set the path as **E:\program files\JAVA\net\simplestream> set path=E:\program files\JAVA\bin** Then compile it as **javac SimpleStream.java**. Then run using **java simplestream.SimpleStream** as you said. But now it shows an error **Error: Could not find or load main class 'simplestream.SimpleStream**, when I run the program. – user3759778 Jun 21 '14 at 06:00
  • When I compile the program it produces two .class files named **SimpleStream.class** and **SimpleStream$1.class** in the package **simplestream**. – user3759778 Jun 21 '14 at 06:13
  • The class is `com.crowley.simplestream.SimpleStream`, not `simplestream.SimpleStream`. The package is `com.crowley.simplestream` as you can see in the source code you provided. – David Conrad Jun 23 '14 at 12:20
  • Thanks for your explanation. Instead of command line, I used IDE now. I got the output. – user3759778 Jun 24 '14 at 12:19