why is Log4j trying to find log4j.dtd

2019-04-05 16:35发布

问题:

I'm getting an exception when I try to run my application (in eclipse) complaining about log4j.dtd. Am I meant to have a dtd file if I use the xml configuration for log4j?

Caused by: java.io.FileNotFoundException: 

    C:\data\workspace\LDICommon_Trunk\resources\log4j.dtd (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)

回答1:

If you are using XML-based log4j, then yes, you will need the DTD file. Copy this DTD file into your workspace: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd

Make sure it sits at the same directory as your log4j XML file.



回答2:

Check your log4j.xml it must be looking for log4j.dtd

<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">

Instead you can use:

<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

source



回答3:

If you use an XML file as log4j configuration file, look in the DOCTYPE tag and you will find something like this:

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

So you need that DTD file, because it is the definition of the structure of your XML.

Anyway you can use a properties file as configuration file, instead of an XML.

EDIT: I've just found a correlated question that could help you: Eclipse: Referencing log4j.dtd in log4j.xml



回答4:

For people coming on that, as of now, log4j.dtd is included in log4j.jar (in version 1.2.14 at least).

As log4j.dtd is looked for in your classpath (see this other question) you shouldn't need it anymore.



回答5:

Ok, I got the answer to my problem, it turns out log4j doesn't need the dtd file to be located in your project or machine.

The error was caused by eclipse trying to run the log4j.xml file when I launched a run. Previously eclipse used to run the last run configuration, now it tries to run whatever is open and in focus in the editor. And it just happened that my log4j.xml configuration file was open, and whatever eclipse uses to run an xml file was trying to get to the dtd as per the configuration file.

Anyway, thanks a lot for your help.



回答6:

i had this problem with a jar application and ended up putting log4j.dtd in the directory hierarchy and the warn/error messages disappeared:

org/apache/log4j/xml/log4j.dtd

btw that is the location in the log4j.jar file



标签: java log4j