0

我正在寫一個實用工具類使用twitter4j多種用途。我可以根據Hashtags,位置等搜索推文,但Streaming API不適合我。Twitter4j流API調用拋出類沒有發現異常

我跟着一個博客後寫了一個類,如下所示,但是我沒有找到類。error.I是java的新手。

package mytweetapp; 

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

public class Stream { 
    public static void main(String[] args) throws TwitterException { 

ConfigurationBuilder cb = new ConfigurationBuilder(); 
cb.setDebugEnabled(true) 
     .setOAuthConsumerKey("*****") 
     .setOAuthConsumerSecret(
       "*******") 
     .setOAuthAccessToken(
       "*****") 
     .setOAuthAccessTokenSecret(
       "*****"); 

TwitterStreamFactory tf = new TwitterStreamFactory(cb.build()); 
TwitterStream twitter = tf.getInstance(); 

StatusListener listener = new StatusListener() { 

    public void onStatus(Status status) { 
     System.out 
       .println("@" + status.getUser().getScreenName() + " - " + status 
         .getText()); 
    } 

    public void onDeletionNotice(
      StatusDeletionNotice statusDeletionNotice) { 
     System.out 
       .println("Got a status deletion notice id:" + statusDeletionNotice 
         .getStatusId()); 
    } 

    public void onTrackLimitationNotice(int numberOfLimitedStatuses) { 
     System.out 
       .println("Got track limitation notice:" + numberOfLimitedStatuses); 
    } 

    public void onScrubGeo(long userId, long upToStatusId) { 
     System.out 
       .println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); 
    } 

    public void onException(Exception ex) { 
     ex.printStackTrace(); 
    } 

    @Override 
    public void onStallWarning(StallWarning arg0) { 
     // TODO Auto-generated method stub 

    } 
}; 

FilterQuery fq = new FilterQuery(); 
String keywords[] = { "Mango", "Banana" }; 

fq.track(keywords); 

twitter.addListener(listener); 
twitter.filter(fq); 
} 

錯誤

Exception in thread "main" java.lang.NoClassDefFoundError: twitter4j/internal/http/HttpClientWrapperConfiguration 
    at twitter4j.TwitterStreamFactory.<clinit>(TwitterStreamFactory.java:40) 
    at mytweetapp.Stream.main(Stream.java:23) 
Caused by: java.lang.ClassNotFoundException: twitter4j.internal.http.HttpClientWrapperConfiguration 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 2 more 
+0

你是從IDE/mvn cli裏面運行的嗎?你可以請把整個項目上傳到github,這樣就可以回答上下文問題(例如,你有哪些版本,你的類路徑,...) – Ivan

+0

@Ivan我在eclipse上運行這個類,調用主方法 twitter4j-core-4.0.4.jar twitter4j-stream-3.0.3.jar是罐子版本 你問的是什麼類路徑是這個類的嗎?我是新來的這個 – 2FaceMan

+0

你可以上傳整個文件夾與.jar,.project,.classpath,.java和其他文件,例如github讓其他人更容易調試。 – Ivan

回答

2

我現在的假設是,這個問題是因爲從流3.0.3混合不同版本的核心和流罐(所以如TwitterStreamFactory的預計HttpClientWrapperConfiguration可用在你的類路徑中,但是在4.0.4中,它不再被包含,請嘗試包含相同版本的版本(並且只有一個版本的lib,所以stream-3和stream-4被包含在一起是不可以的)。將無法工作 - 分享整個項目的更多上下文

至於什麼類路徑是你可以谷歌,或讀取例如這裏是What is a classpath?

+0

我上傳了gDrive上的項目,因爲我無法放在github上。 https://drive.google.com/file/d/0B0gxtpfo7Zs1dnk1QmZiZTUwY28/view?usp=sharing – 2FaceMan

+1

你是對的,jar版本是問題所在。謝謝 :) – 2FaceMan