2

I am starting to learn mahout, but in the first step I got a strange error in my program. I'm trying to build a simple recommender in a few lines:

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);
        }
    }
}

but I get this error

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

what is the problem with this code? I copied it from a book!


But using all those jar files I get the following error:

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

am I using extra jar files?

Taryn
  • 242,637
  • 56
  • 362
  • 405
orezvani
  • 3,595
  • 8
  • 43
  • 57
  • 1
    In general, when you write a program that depends on other software, you need to include all the software that *it* depends on when you run it. This is no different. – Sean Owen Nov 08 '12 at 15:21

3 Answers3

3

You would require the jars from the slf4j project in your classpath.

Download it from : http://www.slf4j.org/download.html

Santosh Gokak
  • 3,393
  • 3
  • 22
  • 24
  • @emab Is it again NoClassDefFoundError? – Santosh Gokak Nov 08 '12 at 15:36
  • but I think the code is OK the problem might be with the LoggerFactory! – orezvani Nov 08 '12 at 15:49
  • As mentioned at http://www.slf4j.org/codes.html#jclDelegationLoop, you should remove jcl-over-slf4j.jar from the classpath and try. I hope you have come logging implantation in place which SLFJ can delegate. – Santosh Gokak Nov 08 '12 at 15:51
  • what is "Class path contains multiple SLF4J bindings." for?! – orezvani Nov 08 '12 at 16:02
  • http://stackoverflow.com/questions/7571506/how-to-suppress-slf4j-warning-about-multiple-bindings this should give you a hint – Santosh Gokak Nov 08 '12 at 16:14
  • You're including way too much of slf4j, yes. You are not supposed to use more than one binding. Just use the libraries that Mahout uses rather than guess through it. – Sean Owen Nov 09 '12 at 07:52
2

Please, take source code, or at least, pom.xml from the repository with source code for examples. This pom.xml specifies all dependencies, and everything was tested for work with different Mahout's versions. Please, look to this blog post on how to start to work with examples.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
0

Thank you all guys.

After so much confusion on this problem, I finally solved the problem! I just web to the mahout directory and added all the jar files of "mahout/utils/target/dependency/" to the classpath.

Since I had used maven to install mahout, I could also use jar files in ".m2/" folder for the dependencies but the first solution was much simpler.

orezvani
  • 3,595
  • 8
  • 43
  • 57
  • it's better to use Maven's projects, instead of projects of your IDE - much easier to manipulate with dependencies & your project could be built without IDE, etc. – Alex Ott Nov 09 '12 at 12:38
  • But how can I do that? I'm not familiar with it! – orezvani Nov 11 '12 at 09:28
  • You can take copy of `pom.xml` from examples, and then start to add your code. If I remember, Eclipse allows create Maven project directly - you just need to install `m2e` plugin – Alex Ott Nov 11 '12 at 10:19