提问者:小点点

线程“main”java. lang.NoClassDefFoundError中的异常:org/apache/Commons/csv/CSVFormat


我正在为log4j2使用gradle项目…并且我在创建csv布局时收到以下错误:线程“main”java. lang.NoClassDefFoundError中的异常:org/apache/Commons/csv/CSVFormat

CsvParameterLayout示例类

package log4j_tutorial;
import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

public class CsvParameterLayoutExample {

private static final Logger logger = LogManager.getLogger();

public static void main(String[] args) {
    int val1 = 10, val2 = 11, val3 = 12;
    logger.trace("Trace Message!", val1, val2, val3);
    logger.debug("Debug Message!", val1, val2, val3);
    logger.info("Info Message!", val1, val2, val3);
    logger.warn("Warn Message!", val1, val2, val3);
    logger.error("Error Message!", val1, val2, val3);
    logger.fatal("Fatal Message!", val1, val2, val3);
}

}

log4j2. xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" xmlns="http://logging.apache.org/log4j/2.0/config" packages="log4j_tutorial">
<Appenders>
<File name="my_file_appender"  fileName="C:/Users/bishal.gupta/Desktop/Testing/CsvLog4jTest/application.log">
<CsvParameterLayout delimiter=","/>
</File>
</Appenders>

<Loggers>
   <Root level="info">
       <AppenderRef ref="my_file_appender" />
   </Root>
</Loggers>
</Configuration>

build. gradle用于依赖项

例外:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getDeclaredMethods(Class.java:1808)
at     org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.createBuilder(PluginBuilder.java:149)
at   org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:119)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:888)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:828)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:820)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:820)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:449)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:197)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:209)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:492)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:562)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:578)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:214)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:145)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:182)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:455)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:441)
at log4j_tutorial.CsvParameterLayoutExample.<clinit>(CsvParameterLayoutExample.java:7)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.csv.CSVFormat
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 22 more

我从早上就被困住了…:(


共2个答案

匿名用户

您必须添加Comms-csv作为依赖项。

如果您在标记问题时使用gradle,请添加以下行:

// https://mvnrepository.com/artifact/org.apache.commons/commons-csv
compile group: 'org.apache.commons', name: 'commons-csv', version: '1.4'

匿名用户

build. gradle无法下载我提到的jar:

build. gradle:"compile'org.apache.Commons:Comms-csv:1.1"

所以我从下面的链接下载了公共csvjar并添加到项目外部

公地-csv. jar

二进制文件:commons-csv-1.4-bin.zip