使用mahout的NoClassDefFoundError

时间:2012-11-08 15:06:54

标签: java mahout recommendation-engine mahout-recommender

我开始学习mahout,但是在第一步中我的程序中出现了一个奇怪的错误。 我试图用几行来构建一个简单的推荐器:

import java.util.List;
import java.io.File;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
/**
 */
public class RecommenderIntro {
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        DataModel model =
                new FileDataModel (new File("intro.csv"));
        UserSimilarity similarity =
                new PearsonCorrelationSimilarity (model);
        UserNeighborhood neighborhood =
                new NearestNUserNeighborhood (2, similarity, model);
        Recommender recommender = new GenericUserBasedRecommender (
                model, neighborhood, similarity);
        List<RecommendedItem> recommendations =
                recommender.recommend(1, 1);
        for (RecommendedItem recommendation : recommendations) {
            System.out.println(recommendation);
        }
    }
}

但我收到此错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.apache.mahout.cf.taste.impl.model.file.FileDataModel.<clinit>(FileDataModel.java:119)
    at recommenderintro.RecommenderIntro.main(RecommenderIntro.java:28)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    ... 2 more
Java Result: 1

这段代码有什么问题?我从书中复制了它!


但是使用所有这些jar文件我收到以下错误:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/omid/Desktop/slf4j-1.7.2/slf4j-jcl-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/omid/Desktop/slf4j-1.7.2/slf4j-jdk14-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/omid/Desktop/slf4j-1.7.2/slf4j-log4j12-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/omid/Desktop/slf4j-1.7.2/slf4j-nop-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/omid/Desktop/slf4j-1.7.2/slf4j-simple-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Detected both jcl-over-slf4j.jar AND slf4j-jcl.jar on the class path, preempting StackOverflowError. 
SLF4J: See also http://www.slf4j.org/codes.html#jclDelegationLoop for more details.
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:73)
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:42)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:107)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:295)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:269)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
    at org.apache.mahout.cf.taste.impl.model.file.FileDataModel.<clinit>(FileDataModel.java:119)
    at recommenderintro.RecommenderIntro.main(RecommenderIntro.java:28)
Caused by: java.lang.IllegalStateException: Detected both jcl-over-slf4j.jar AND slf4j-jcl.jar on the class path, preempting StackOverflowError. See also http://www.slf4j.org/codes.html#jclDelegationLoop for more details.
    at org.slf4j.impl.JCLLoggerFactory.<clinit>(JCLLoggerFactory.java:55)
    ... 9 more
Java Result: 1

我使用额外的jar文件吗?

3 个答案:

答案 0 :(得分:3)

您需要来自类路径中slf4j项目的jar。

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

下载

答案 1 :(得分:2)

请从the repository with source code for examples获取源代码,或至少pom.xml。这个pom.xml指定了所有依赖项,并且测试了所有内容以使用不同的Mahout版本。请查看this blog post如何开始使用示例。

答案 2 :(得分:0)

谢谢大家。

经过这么多关于这个问题的困惑,我终于解决了这个问题!我只是访问mahout目录,并将"mahout/utils/target/dependency/"的所有jar文件添加到类路径中。

由于我使用maven来安装mahout,我也可以在".m2/"文件夹中使用jar文件来获取依赖项,但第一种解决方案要简单得多。