【问题标题】:Getting exception in initializing log4j files在初始化 log4j 文件时出现异常
【发布时间】:2011-08-30 15:09:29
【问题描述】:

当尝试从配置文件初始化休眠时,我得到 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

发布跟踪时出现对齐问题,抱歉,无法纠正该问题..

【问题讨论】:

  • 异常中的堆栈跟踪不足,请全部提供。无论如何,在这里使用静态单例是非常糟糕的主意,使用 Spring 或类似的进行依赖注入。此外,hibernate 正在抱怨 apache commons,而不是像您的测试用例中那样抱怨 jul。我怀疑其他类中的类加载器和静态初始化块存在一些问题。
  • 是的,我完全同意,但这只是一些测试代码.. 使用 n throw 之类的.. 所以忽略了设计方面。顺便说一句,我不认为这是由于静态块,因为我在其他环境中使用过它。是的,这是 apache commons 中的一个问题,所以我也将其标记为日志记录
  • 我添加了eclipse作为标签,这个问题在使用eclipse运行测试套件时非常具体。如果使用 ant 脚本,则不会发生这种情况。

标签: eclipse hibernate logging apache-commons-logging


【解决方案1】:

最后,我能够解决这个问题。正如后来猜测的那样,它原来是一个eclipse配置。在 eclipse 3.7 中,代号 indigo,将库添加到类路径的方式导致了这个问题..

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

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

【讨论】:

  • 正确的方法是使用 Maven ;)
  • 是的,我在生产中使用它(不是 maven,而是 ant),并且如前所述,它也运行良好.. 就像我从 eclipse 中尝试过的一样易用.. 它坏了.. 是只是古玩来修复它:)
  • 你应该使用Maven,它和ant不一样,它是更高级的依赖管理工具。可以在 Eclipse 中导入 maven 项目,自动为您构建所有类路径/等内容,避免人为错误。
猜你喜欢
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 2011-12-02
  • 1970-01-01
相关资源
最近更新 更多