SLF4J在尝试在Java中使用themoviedbapi时出现错误。

huangapple go评论78阅读模式
英文:

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(&quot;APIKEY&quot;).getMovies();
MovieDb movie = movies.getMovie(5353, &quot;en&quot;);

I get this error,

Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
	at info.movito.themoviedbapi.tools.WebBrowser.&lt;clinit&gt;(WebBrowser.java:27)
	at info.movito.themoviedbapi.TmdbApi.&lt;init&gt;(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:

&lt;!-- SLF4J API --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
    &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
    &lt;version&gt;1.7.30&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
   &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
   &lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
   &lt;version&gt;1.7.30&lt;/version&gt;
&lt;/dependency&gt;

&lt;!-- LOG4J --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
    &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
    &lt;version&gt;1.7.30&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

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

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

	&lt;properties&gt;
		&lt;java.version&gt;11&lt;/java.version&gt;
	&lt;/properties&gt;

	&lt;repositories&gt;
		&lt;repository&gt;
			&lt;id&gt;jcenter&lt;/id&gt;
			&lt;name&gt;jcenter&lt;/name&gt;
			&lt;url&gt;https://jcenter.bintray.com&lt;/url&gt;
		&lt;/repository&gt;
	&lt;/repositories&gt;

	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;info.movito&lt;/groupId&gt;
			&lt;artifactId&gt;themoviedbapi&lt;/artifactId&gt;
			&lt;version&gt;1.10&lt;/version&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;
&lt;/project&gt;

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(&quot;APIKEY&quot;).getMovies();
		MovieDb movie = movies.getMovie(5353, &quot;en&quot;);
		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 &quot;main&quot; 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.&lt;init&gt;(TmdbApi.java:54)
	at info.movito.themoviedbapi.TmdbApi.&lt;init&gt;(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.

huangapple
  • 本文由 发表于 2020年10月17日 07:59:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64397824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定