我已经尝试在我的项目中将这个工作了三天,并创建了一个简单的项目进行测试 . 我在这里搜索了非常类似的问题,但没有找到任何有助于解决我得到的错误的东西 . 我甚至重新格式化我的Fedora 25,因为它适用于Windows 10,但仍然没有 . 我也在Intellij-2016.3.4注释处理中启用了设置和其他设置,根据其他答案应该修复它但是,它不会改变任何东西 . 请任何帮助将不胜感激!
线程“main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory at com.sammy.CheckLog . (CheckLog.java:8)引起:java.lang.ClassNotFoundException:org.slf4j.LoggerFactory at java.net .URLClassLoader.findClass(URLClassLoader.java:381)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)sun.misc .Launcher $ AppClassLoader.loadClass(Launcher.java:331)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)... 1 more
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CheckLog {
public static void main(String... args){
log.info("I'm Here!! ");
}
}
下面是我的build.gradle文件,其中定义了lombok以及相关的slf4j依赖项 .
/*
* This build file was generated by the Gradle 'init' task.
*
* This generated file contains a commented-out sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/3.4/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'com.sammy.CheckLog'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compileOnly 'org.slf4j:slf4j-api:1.7.24'
compileOnly 'org.slf4j:slf4j-simple:1.7.24'
compileOnly 'org.projectlombok:lombok:1.16.14'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
UPDATE :更改为"compile"后,它在命令行上运行,而不是来自Intellij,因为它似乎使用了's own runtime to interpret it while gradle uses the one I' ve定义 . jdk1.8.0_121
3 回答
问题是您没有在运行时添加SL4J JAR文件,因为您将其用于仅编译 . 将SLF4J依赖项的build.gradle文件更改为
compile
而不是compileOnly
:我有一些问题 . IntelliJ Idea 2016.3和Gradle 3.4.1
初始化一个新项目:
gradle init --type java-application
添加了几行到build.gradle
compile 'org.slf4j:slf4j-api:1.7.24' compile 'org.slf4j:slf4j-simple:1.7.24'
添加到App.java的行:
... static Logger logger = LoggerFactory.getLogger(App.class); ...
现在你在Idea中有错误,但在终端项目中运行正常(使用gradle运行)
Error in IntelliJ Idea
更新:我发现如果我使用gradle 3.3(而不是3.4.1)初始化项目,那么错误就会离开 . 也许这种行为与此问题有关:https://youtrack.jetbrains.com/issue/IDEA-167412
更改:
compileOnly 'org.slf4j:slf4j-api:1.7.24'
至compile 'org.slf4j:slf4j-api:1.7.24'