gpt4 book ai didi

java - 将 slf4j 与 maven 集成

转载 作者:搜寻专家 更新时间:2023-11-01 02:43:40 26 4
gpt4 key购买 nike

大家好,我是 Maven 的新手,我正在尝试将 slf4j 集成到 Maven 项目中这是我的 pom.xml 文件

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.9</version>
</dependency>

我的主函数中有这两行

Logger logger = LoggerFactory.getLogger(App.class);
logger.info("Hello World");

项目编译打包成功,但是当我尝试运行它时通过
java -cp target/maven-1.0-SNAPSHOT.jar com.goutam.maven.App抛出以下异常

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.goutam.maven.App.main(App.java:11)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

最佳答案

build fat jar with maven

将此添加到您的 pom.xml

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>

</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

然后运行

java -cp target/maven-jar-with-dependencies-1.0-SNAPSHOT.jar com.goutam.maven.App

注意:选择具有 jar-with-dependencies

的 jar 文件

关于java - 将 slf4j 与 maven 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27669834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com