英文:
SLF4J Error When Trying to Use themoviedbapi in Java
问题
我正在创建一个项目,将使用The Movie Database API。我尝试使用这个包装器:https://github.com/holgerbrandl/themoviedbapi。我从这里下载了jar文件:https://mvnrepository.com/artifact/info.movito/themoviedbapi/1.10,并将其添加为我的Intellij项目的依赖项。我正在使用Java 8。
当我运行这段代码时,
TmdbMovies movies = new TmdbApi("APIKEY").getMovies();
MovieDb movie = movies.getMovie(5353, "en");
我收到以下错误信息,
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
如何修复这个错误?谢谢。
英文:
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.
答案1
得分: 1
<!-- 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>
一旦您添加了这些依赖项,请确保从Maven执行clean build
以实际从Maven的远程存储库中下载这些依赖项。
英文:
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.
答案2
得分: 1
如果您已经设置好了适当的Maven项目,那么只需添加themoviedbapi
Maven包的依赖就足够了。这将导致Maven引入库所需的所有依赖项,包括slf4j和log4j库。
但是您提到您“下载了jar文件”,这表明您根本没有使用Maven,或者至少不完全在使用。如果您不使用Maven,您将需要手动下载一些API库所需的不同的jar文件。最好还是正确地使用Maven进行设置。
作为对第一个答案的评论,您提到您正在使用Maven,所以我不太确定出了什么问题。我刚刚建立了一个最基本的Maven项目,然后能够很好地运行示例代码。它抱怨没有正确的API密钥,这意味着它正在工作。
构建此项目只需要两样东西...一个主Java类文件和一个pom.xml
文件。为您的项目创建一个目录,然后将此pom.xml
文件放入其中:
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>
然后将此文件保存在创建目录层次结构所示的位置:
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);
}
}
然后,如果您在使用IDE,将pom.xml
文件作为项目加载进去。
如果您已经配置了Maven在命令提示符下工作,只需进入您的项目目录并键入:
mvn package
项目应该会构建。然后您可以使用以下命令运行它:
java -jar target/demo-0.0.1-SNAPSHOT.jar com.example.demo.MainApplication
以下是我运行代码时得到的结果:
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)
这实际上是件好事。这意味着演示代码正在运行,但需要配置一个API密钥才能正常运行。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论