初始化 log4j 文件时出现异常

发布于 2024-12-02 03:44:00 字数 5217 浏览 1 评论 0原文

当尝试从配置文件初始化 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

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

When trying to initialize hibernate from configuration file, I get NullPointerException ..
Root cause is shown as

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)

I have all the logging related jars and properties files in class path.

My hibernate initialization code is :

   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);
    }

Simple java classes does not complain about log related error, for example following java class does not complain ..It properly initializes and writes to logs folder

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;
}

Since it works for stand alone Java class and not for class doing hibernate initialization --I am more perplexed ..Any pointer? Detailed trace

        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

There is alignment issue while posting trace, sorry abt that, not able to correct that one..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2024-12-09 03:44:00

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

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

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

Finally, I was able to solve this. As later guessed it turned out to be an eclipse configuration. In eclipse 3.7, code name indigo, the way libs were being added to the classpath caused this issue ..

  • First way:
    Debug Configurations->classpath->bootstarp entries, if Add libs added by selecting "Add Jars" options, THIS DOES NOT Work.
  • Correct WAY:
    Define a classpath variable from window->preferences->java->buildpath->classpath variable, say HIBERNATE_LIBS and point it ti libs folder
    Now, got to Debug Configurations->classpath->bootstarp entries, select "Advanced option" and add the class path variable. This FIXED the issue.

I guess this is specific to 3.7, as I upgraded the eclipse version and started getting this issue, First way still works in eclipse 3.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文