初始化 log4j 文件时出现异常

2024-03-19

当尝试从配置文件初始化 hibernate 时,我得到 NullPointerException .. 根本原因如图所示

Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)

我在类路径中拥有所有与日志记录相关的 jar 和属性文件。

我的休眠初始化代码是:

   private static final SessionFactory sessionFactory;
private static final ThreadLocal<Session> hibernateSession=new ThreadLocal<Session>();

static{
    try{
        sessionFactory=new Configuration().configure("llhs_hibernate.cfg.xml").buildSessionFactory();
        System.out.println("done");
    }catch (HibernateException he){
        he.printStackTrace();
        throw new ExceptionInInitializerError(he);
    }catch (Exception e){
        e.printStackTrace();
        throw new ExceptionInInitializerError(e);
    }

简单的java类不会抱怨与日志相关的错误,例如以下java类不会抱怨..它正确初始化并写入日志文件夹

public class MetadataService implements Serializable {
private static final Logger logger = LogManager.getLogger(MetadataService.class);
private static final long serialVersionUID = 1L;
public MetaData  getMetaData(){
    logger.info("Info");
    try {
        //some code here
    } catch (Exception e1) {
        logger.error("Error");
    }

    return metaData;
}

因为它适用于独立的 Java 类,而不适用于进行休眠初始化的类——我更困惑了..有任何指针吗?详细痕迹

        java.lang.ExceptionInInitializerError
    at com.llhs.persistence.impl.db.hibernate.utils.HibernateUtils.<clinit>(HibernateUtils.java:17)
    at com.llhs.persistence.impl.hibernate.HibernateDbImplTest.addStudentDetails(HibernateDbImplTest.java:61)
    at com.llhs.persistence.impl.hibernate.HibernateDbImplTest.testAddFindStudentDetails(HibernateDbImplTest.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException(Caused by java.lang.NullPointerException))
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
    at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:126)
    ... 26 more
Caused by: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
    at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
    ... 30 more
Caused by: java.lang.NullPointerException
    at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
    ... 31 more

发布跟踪时存在对齐问题,对此表示抱歉,无法纠正该问题。


最后,我能够解决这个问题。后来猜想,原来是一个eclipse配置。在 eclipse 3.7 中,代码名称 indigo,将库添加到类路径的方式导致了此问题。

  • 第一种方式: 调试配置 -> 类路径 -> bootstarp 条目,如果通过选择“添加 Jars”选项添加添加库,则这不起作用。
  • 正确的方法: 从 window->preferences->java->buildpath->classpath 变量定义一个类路径变量,比如HIBERNATE_LIBS 并将其指向 libs 文件夹 现在,进入“调试配置”->“类路径”->“bootstarp 条目”,选择“高级选项”并添加类路径变量。这解决了这个问题。

我猜这是 3.7 特有的,因为我升级了 eclipse 版本并开始遇到此问题,第一种方法在 eclipse 3.2 中仍然有效

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

初始化 log4j 文件时出现异常 的相关文章

随机推荐