4

I am making an app that communicates with Twitter to post a tweet. My problem is that when compile my code, I get the error message:

NoClassDefFoundError: twitter4j.TwitterFactory

Here is the code that post's the tweet via an onClickListener:

post.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Twitter twitter = new TwitterFactory().getInstance();  
            AccessToken a = new AccessToken("****", "****");
            twitter.setOAuthConsumer("****", "****");
            twitter.setOAuthAccessToken(a); 
            try {
                twitter.updateStatus("If you're reading this on Twitter, it worked!");
            } catch (TwitterException e) {
                e.printStackTrace();
            }   
        }
    });

Here is what I got in my logcat:

04-04 10:27:51.330: E/AndroidRuntime(12547): FATAL EXCEPTION: main
04-04 10:27:51.330: E/AndroidRuntime(12547): java.lang.NoClassDefFoundError: twitter4j.TwitterFactory
04-04 10:27:51.330: E/AndroidRuntime(12547):    at com.bintuu.updater.TwitterClass$3.onClick(TwitterClass.java:107)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at android.view.View.performClick(View.java:2485)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at android.view.View$PerformClick.run(View.java:9080)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at android.os.Handler.handleCallback(Handler.java:587)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at android.os.Looper.loop(Looper.java:130)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at android.app.ActivityThread.main(ActivityThread.java:3683)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at java.lang.reflect.Method.invoke(Method.java:507)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 10:27:51.330: E/AndroidRuntime(12547):    at dalvik.system.NativeStart.main(Native Method)
Buneme Kyakilika
  • 1,202
  • 3
  • 13
  • 34
  • Have you added the Twitter4j jar to your classpath? See: http://www.vogella.de/articles/Eclipse/article.html#classpath_jar – Eric Levine Apr 04 '12 at 02:40
  • Yes I have. None the less, I still experience the same problem. By that tutorial is aimed at plain Java. Im using androids java. – Buneme Kyakilika Apr 04 '12 at 12:03
  • Its the same steps for Android and plain Java projects. – Eric Levine Apr 04 '12 at 13:13
  • Yes, I've acknoleged that, but I still get the same errot. – Buneme Kyakilika Apr 04 '12 at 15:05
  • Can you provide more details regarding this error? Is there a stack trace that goes along with it? Can you be more specific than saying it happens when you compile your code? Is Eclipse underlining it in red and giving you this error, or does it happen while you are running your application? – Eric Levine Apr 04 '12 at 15:23
  • When I run the app, I get that message in the log cat. – Buneme Kyakilika Apr 04 '12 at 15:50
  • Ok, so its not a compile error but rather a runtime error. Can you add the full stack trace you are getting in logcat into your original post? This should include information about which lines in your code are causing the error. – Eric Levine Apr 04 '12 at 17:08
  • I am also facing the same problem. – wasaig Apr 05 '12 at 10:53
  • found the solution in this [answer](http://stackoverflow.com/questions/10005206/twitter4j-androidruntime446-java-lang-noclassdeffounderror-twitter4j-http) – wasaig Apr 05 '12 at 11:10
  • @wasaig Does not help. I get a red exclamation mark on my project in my hiecary. I only have one jar in my lib folder. – Buneme Kyakilika Apr 06 '12 at 03:48
  • if you are using ADT 17 rename your folder from lib to libs – wasaig Apr 06 '12 at 04:27

6 Answers6

4

try renaming the "lib" folder name to "libs". That solved a similar issue for me, check here: https://github.com/ddewaele/AndroidTwitterSample/issues/2

PakitoV
  • 2,476
  • 25
  • 34
2

I've found the solution now. The problem was that I was using a very old version of Twitter4j, and in my libs folder, there were multiple jars with identical names. I have now fixed those problems and it is now working perfectly.

Buneme Kyakilika
  • 1,202
  • 3
  • 13
  • 34
1

Create a folder named libs inside the root directory of your project and put twitter4j jar inside.

Then include new jar via Project Properties -> Java Build Path -> Libraries -> Add Jars.

Remove any old reference to twitter4j jars.

Clean and launch.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
MoJa
  • 7
  • 2
1

I had the same problem with twitter4j, and I solved it. Simply after naming the folder to "libs", then I made the "libs" folder to be used as the source folder so as some methods within can be accessed. I wonder what is the actual chemistry behind it!! but that's it, it is now working for me. To make it a source folder, you simply right click it, go to the "build path" and then select "Use as source folder".

0

I know it's too old but for those like me - the problem was solved by including log4j, slf4j and commons-logging jars in the project since twitter4j core uses them.

coding_idiot
  • 13,526
  • 10
  • 65
  • 116
0

I found that I Imported Twitter4J the incorrect way. So what I proceeded to do was remove it from my build path then copy twitter4j-core-4.0.2.jar to my libs folder then restart eclipse and add the jar in my libs to my build path. All was sorted thereafter.

MNG MAN
  • 69
  • 1
  • 4