繁体   English   中英

线程“ Twitter4J异步分派器[0]”中的异常java.lang.NoClassDefFoundError

[英]Exception in thread “Twitter4J Async Dispatcher[0]” java.lang.NoClassDefFoundError

我正在从事一个项目,该项目可以分析实时推文并确定用户的心情。 因此,我正在使用twitter4j接收实时鸣叫并将这些鸣叫提供给斯坦福大学的Core NLP。 我正确地收到了实时推文。 但是,当我将这些推文提供给斯坦福大学的Core NLP时,我遇到了运行时错误。

使用twitter4j获取实时鸣叫的PrintSampleStream类:

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import javax.swing.Timer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import twitter4j.*;
import twitter4j.conf.*; 

public class PrintSampleStream {

    private String twitter_handle;

    PrintSampleStream()
    {
        twitter_handle = null;
    }

    PrintSampleStream(String tw)
    {
        twitter_handle = tw;
    }

    public void twitterConnector() throws TwitterException {
         ConfigurationBuilder cb = new ConfigurationBuilder();
          cb.setDebugEnabled(true).setOAuthConsumerKey("bbbb")
                  .setOAuthConsumerSecret("bbbb")
                  .setOAuthAccessToken("bbbb")
                  .setOAuthAccessTokenSecret("bbbb");
          TwitterStream twitterStream = new TwitterStreamFactory(cb.build())
                  .getInstance();
        StatusListener listener = new StatusListener() {
            @Override
            public void onStatus(Status status) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
                NLP.init();
                System.out.println(status.getText() + " : " + NLP.findSentiment(status.getText()));
                //storeTweets(status.getText());
                //JOptionPane.showMessageDialog(null, status.getText());
            }

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

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

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

            @Override
            public void onStallWarning(StallWarning warning) {
                System.out.println("Got stall warning:" + warning);
            }

            @Override
            public void onException(Exception ex) {
                ex.printStackTrace();
            }
        };
        twitterStream.addListener(listener);
        FilterQuery filtre = new FilterQuery();
        String[] keywordsArray = {twitter_handle};
        filtre.track(keywordsArray);
        twitterStream.filter(filtre);
    }
} 

NLP类将从twitter4j接收到的实时推文提供给斯坦福大学的Core NLP:

import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;

public class NLP {
    static StanfordCoreNLP pipeline;

    public static void init() {
        pipeline = new StanfordCoreNLP("MyPropFile.properties");
    }

    public static int findSentiment(String tweet) {

        int mainSentiment = 0;
        if (tweet != null && tweet.length() > 0) {
            int longest = 0;
            Annotation annotation = pipeline.process(tweet);
            for (CoreMap sentence : annotation
                    .get(CoreAnnotations.SentencesAnnotation.class)) {
                Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
                int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
                String partText = sentence.toString();
                if (partText.length() > longest) {
                    mainSentiment = sentiment;
                    longest = partText.length();
                }

            }
        }
        return mainSentiment;
    }
}

我的运行时错误是:

@laliyaD - Lalinda feels tired
Exception in thread "Twitter4J Async Dispatcher[0]" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<clinit>(StanfordCoreNLP.java:99)
    at NLP.init(NLP.java:13)
    at PrintSampleStream$1.onStatus(PrintSampleStream.java:38)
    at twitter4j.StatusStreamImpl.onStatus(StatusStreamImpl.java:75)
    at twitter4j.StatusStreamBase$1.run(StatusStreamBase.java:105)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 8 more

实际上,我是从twitter4j获取实时推文的。 有什么帮助吗?

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory意味着需要在类路径中使用slf4j库。

如果使用maven,则可以使用以下依赖项:

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-simple</artifactId>
 <version>1.7.21</version>
</dependency>

您需要下载SLF4J (Java的简单日志记录门面)并将其包含在类路径中。

您至少需要slf4j-api-1.7.21.jarslf4j-simple-1.7.21.jar才能真正查看来自NLP库的日志消息。

http://www.slf4j.org/download.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM