1. ホーム
  2. java

[解決済み] java.lang.ClassNotFoundException: org.apache.log4j.Level

2022-02-09 23:46:11

質問

swingアプリケーションでhibernate 3.5.1 finalを使用しようとしています。 そして、私が使用しているjarは以下の通りです。

  • hibernate-core-3.5.1-Final
  • hibernate-entitymanager-3.5.1-Final
  • hibernate-jpa-2.0-api-1.0.0.Final
  • hibernate-annotations-3.5.1-Final
  • hibernate-commons-annotations-3.2.0.Final
  • dom4j-1.6.1
  • slf4j-api-1.6.4
  • slf4j-log4j12-1.6.4

アプリケーションを実行しようとすると、以下のエラーが表示されます。

Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/log4j/Level
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:279)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:252)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:265)
    at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:165)
    at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:127)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at com.xeno.xecamp.desktopManagement.Main.main(Main.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    ... 11 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:279)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:252)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:265)
    at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:165)
    at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:127)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at com.xeno.xecamp.desktopManagement.Main.main(Main.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

解決方法は?

また、Log4JのJARファイルをクラスパスに含める必要があります。

なお slf4j-log4j12-1.6.4.jar は、SLF4J API 経由で Log4J を使用できるようにするためのアダプタにすぎません。実際の Log4J の実装は含まれていません。