15

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)
Charbel
  • 14,187
  • 12
  • 44
  • 66
  • 1
    Could you include more of your stack trace, because log4j normally uses an EntityResolver that enables loading the log4j dtd from the classpath (and thus from the log4j jar file) – JB Nizet Feb 11 '11 at 12:33

6 Answers6

29

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.

limc
  • 39,366
  • 20
  • 100
  • 145
  • Thanks limc for helping me to find the log4j.dtd. Not directly related to the question, but I've found your answer very helpful for me. – jbbarquero Feb 19 '13 at 12:52
  • 1
    This link seems not working right now. Do you have a latest working url ? – Suresh Aug 20 '18 at 07:46
18

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

Community
  • 1
  • 1
5

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

Community
  • 1
  • 1
bluish
  • 26,356
  • 27
  • 122
  • 180
  • can I get away without defining a dtd? (while using xml configuration) – Charbel Feb 11 '11 at 12:59
  • 1
    I guess you can't, but you can try to remove `DOCTYPE` tag or only `SYSTEM "log4j.dtd"` part – bluish Feb 11 '11 at 13:04
  • in the log4j wiki http://wiki.apache.org/logging-log4j/Log4jXmlFormat then don't mention anything about log4j.dtd, I can't find any log4j.dtd in other projects. Also removing any part of the – Charbel Feb 11 '11 at 13:14
4

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.

Dag Høidahl
  • 7,873
  • 8
  • 53
  • 66
Charbel
  • 14,187
  • 12
  • 44
  • 66
  • What's the matter if your log4j.xml configuration file was open? I can't understand the logic in your answer. Can you elaborate a bit and make it clear? – Suresh Aug 09 '18 at 05:40
3

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.

Community
  • 1
  • 1
Matthieu
  • 2,736
  • 4
  • 57
  • 87
0

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

Bob
  • 780
  • 9
  • 10