我正试图通过oauth访问twitter。因此,我注册了我的应用程序,下载了Twitter4j,在我的Eclipse-Project中添加了jar,然后尝试执行以下代码:
Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance("[key]","[secretKey]");
RequestToken requestToken = twitter.getOAuthRequestToken();
System.out.println(requestToken.getAuthorizationURL());
但它引发了以下异常:
[Sat May 29 11:19:11 CEST 2010]Using class twitter4j.internal.logging.StdOutLoggerFactory as logging factory.
[Sat May 29 11:19:11 CEST 2010]Use twitter4j.internal.http.alternative.HttpClientImpl as HttpClient implementation.
Exception in thread "main" java.lang.AssertionError: java.lang.reflect.InvocationTargetException
at twitter4j.internal.http.HttpClientFactory.getInstance(HttpClientFactory.java:71)
at twitter4j.internal.http.HttpClientWrapper.<init>(HttpClientWrapper.java:59)
at twitter4j.http.OAuthAuthorization.init(OAuthAuthorization.java:83)
at twitter4j.http.OAuthAuthorization.<init>(OAuthAuthorization.java:74)
at twitter4j.TwitterFactory.getOAuthAuthorizedInstance(TwitterFactory.java:112)
at MainProgram.main(MainProgram.java:18)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at twitter4j.internal.http.HttpClientFactory.getInstance(HttpClientFactory.java:65)
... 5 more
Caused by: java.lang.NoClassDefFoundError: org/apache/http/impl/client/DefaultHttpClient
at twitter4j.internal.http.alternative.HttpClientImpl.<init>(HttpClientImpl.java:63)
... 10 more
我目前无法弄清楚为什么......你能给我一些建议吗?
祝你好运 菲利普
答案 0 :(得分:2)
Caused by: java.lang.NoClassDefFoundError: org/apache/http/impl/client/DefaultHttpClient
您需要将apache http组件添加到类路径
答案 1 :(得分:0)
因为我读'eclipse项目' - 如果这实际上是一个插件/ rcp项目并且你的库驻留在其他插件中,那么你必须通过plugin.xml导出所需的包,否则会导致NoClassDefFound错误。