0

I'm creating a project that will use the The Movie Database API. I am trying you use this wrapper https://github.com/holgerbrandl/themoviedbapi. I downloaded the jar file from here https://mvnrepository.com/artifact/info.movito/themoviedbapi/1.10 and added it as a dependency to my Intellij project. I am using Java 8. When I run this code,

TmdbMovies movies = new TmdbApi("APIKEY").getMovies();
MovieDb movie = movies.getMovie(5353, "en");

I get this error,

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at info.movito.themoviedbapi.tools.WebBrowser.<clinit>(WebBrowser.java:27)
    at info.movito.themoviedbapi.TmdbApi.<init>(TmdbApi.java:44)
    at Test.main(Test.java:7)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

How do I fix this error? Thank you.

PhonyStark
  • 21
  • 2

2 Answers2

1

If you are using Maven for building your project and managing dependencies, add following Maven dependency to download SLF4J and Log4j JAR files into your project:

<!-- SLF4J API -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-simple</artifactId>
   <version>1.7.30</version>
</dependency>

<!-- LOG4J -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.30</version>
    <scope>test</scope>
</dependency>

Once you add these dependencies, make sure you do a clean build from Maven to actually download these dependencies from Maven's remote repository.

1218985
  • 7,531
  • 2
  • 25
  • 31
  • I added these dependencies to my pom.xml file and I now I get a bunch of different errors. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Exception in thread "main" info.movito.themoviedbapi.tools.MovieDbException: Failed to read configuration These are a few errors I'm getting. – PhonyStark Oct 17 '20 at 00:18
  • Try adding slf4j-simple as given in the answer. – 1218985 Oct 17 '20 at 00:34
1

If you have a proper Maven project already set up, then just adding the dependency for the themoviedbapi Maven package should be enough. It will cause Maven to bring in all of the dependencies needed by the library, including the slf4j and log4j libraries.

But you say you "downloaded the jar file", which suggests you aren't using Maven at all, or at least not entirely. If you don't use Maven, you're going to need to manually download a few different jar files that is API library requires. Better to get set up properly with Maven.

As a comment to the first answer, you suggest that you are using Maven, so I'm not sure what's going on. I just set up the most basic of Maven projects, and I was able to run the sample code just fine. It complains about not having a proper API key, which means it is working.

You need just two things to build this with Maven...a main Java class file and a pom.xml file. Create a directory for your project, then put this pom.xml file in it:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>info.movito</groupId>
            <artifactId>themoviedbapi</artifactId>
            <version>1.10</version>
        </dependency>
    </dependencies>
</project>

Then save this file at the location indicated by creating the directory hierarchy shown here:

src/main/java/com/example/demo/MainApplication.java

package com.example.demo;

import info.movito.themoviedbapi.TmdbApi;
import info.movito.themoviedbapi.TmdbMovies;
import info.movito.themoviedbapi.model.MovieDb;

public class DemoApplication {

    public static void main(String[] args) {
        TmdbMovies movies = new TmdbApi("APIKEY").getMovies();
        MovieDb movie = movies.getMovie(5353, "en");
        System.out.println(movie);
    }
}

Then, if you're using an IDE, load the pom.xml file into it as a project.

If you have Maven configured to work at a command prompt, just cd into your project directory and type:

mvn package

The project should build. Then you can run it with:

java -jar target/demo-0.0.1-SNAPSHOT.jar com.example.demo.MainApplication

Here's what I got when I ran the code:

Exception in thread "main" ResponseStatus{code=7, message=Invalid API key: You must be granted a valid key.}
    at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:78)
    at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:45)
    at info.movito.themoviedbapi.AbstractTmdbApi.mapJsonResult(AbstractTmdbApi.java:40)
    at info.movito.themoviedbapi.TmdbConfig.getConfig(TmdbConfig.java:18)
    at info.movito.themoviedbapi.TmdbApi.<init>(TmdbApi.java:54)
    at info.movito.themoviedbapi.TmdbApi.<init>(TmdbApi.java:44)
    at com.example.demo.DemoApplication.main(DemoApplication.java:10)

This is actually a good thing. This means the demo code is running, but needs to be configured with an API key to run properly.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Thank you for your detailed explanation. I did what you explained but I'm getting this error SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. – PhonyStark Oct 17 '20 at 01:30
  • Interesting. I don't know why you get that and I don't. If you didn't change my version of the `pom.xml`, it does show you that you don't need to include the `slf4j` modules explicitly, as Maven will include them for you. But in the end, you've got the same problem either way. Strange. I don't know what to tell you. I'd suggest Googling for that error message or waiting for someone else to come along here who has seen that before. Best of luck. – CryptoFool Oct 17 '20 at 01:42
  • Hey. This SO answer says that @1218984 was on the right track...maybe even right. I stand corrected. I guess there's an issue with implicitly relying on `slf4j` in another Maven module. Anyway...check this out: https://stackoverflow.com/questions/7421612/slf4j-failed-to-load-class-org-slf4j-impl-staticloggerbinder ... and maybe try adding just the first two dependencies listed in the other answer. – CryptoFool Oct 17 '20 at 01:46