121

I've been using log4j for quite a while now and I usually use this at the top of the log4j.xml (probably just like many others and according to Google this is the way to do it):

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

Obviously this is working, however Eclipse doesn't provide its context-sensitive help for writing the XML and all. Furthermore, it always shows a warning that it doesn't find the log4j.dtd. Now I'm curious how to fix this.

I tried a few things and these work:

<!DOCTYPE log4j:configuration SYSTEM "jar:file:/path/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar!/org/apache/log4j/xml/log4j.dtd">
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

As you can see from above we're using Maven. Therefore, I tried this, but it fails:

<!DOCTYPE log4j:configuration SYSTEM "jar:file:${M2_REPO}/log4j/log4j/1.2.14/log4j-1.2.14.jar!/org/apache/log4j/xml/log4j.dtd">

Eclipse usually knows how to deal with the classpath variables, but why doesn't this work? I know that the reference won't work during runtime, but neither does a simple log4j.dtd (if I'm not wrong), so that shouldn't be a problem.

Can anyone please shed a light on this?

Werd
  • 47
  • 7
sjngm
  • 12,423
  • 14
  • 84
  • 114

7 Answers7

188

I know this question has been answered, but I'd like to provide my slightly different alternative:

<!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">

It is similar to @FrVaBe's response, but on the plus side, does not require any further Eclipse configuration (i.e., if you're sharing your project with others, or have a large team, it's one less thing to worry about).

The down side though, is that I believe it means you'll need an Internet connection (at least at some point during development, even if it's just one time).

Community
  • 1
  • 1
Jack Leow
  • 21,945
  • 4
  • 50
  • 55
  • this approach is better than the accepted answer, since you don't have to deal with physical location of dtd file. This helps when sharing your project with other developers. – aces. Jan 09 '13 at 23:15
  • 1
    Interesting. Now I have to find the dtd for log4j 2.0 — what pain — what my property file configuration back! – Martin Sep 09 '14 at 11:16
  • 8
    sometime recently this has stopped working. If you go in a browser to http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd you'll see that there is now an xml error – Kip Sep 26 '16 at 12:49
  • @Kip you are right, link is not working. Got any updated link with anyone here? – Suresh Aug 20 '18 at 07:36
  • I checked the link with curl and it returned a complete DTD. If I tried to look at it in a browser, it returns errors. I would suggest checking it with tool like curl. – John Yeary Jan 28 '19 at 20:16
42

Try to add the log4j.dtd as a User Specific URI XML Catalog Entry in "Preferences -> XML -> XML Catalog". As I know this is the place where eclipse manages the references to definition/validation files (like xsd). If they can be found here eclipse needs no internet access to access them on their native (web) location.

I did it like this (for test) and eclipse does not complain:

Entry element:    URI
Location:         C:\Users\me\Desktop\log4j.dtd
URI:              file:///C:/Users/me/Desktop/log4j.dtd
Key type:         URI
Key:              http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd

Maybe also ${M2_REPO} works - I did not check this.

Use the native URL in your log4j.xml afterwards

<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

EDIT

I would go with the above solution but to come back to your question, I think class path variables 'can be used in a Java Build Path'. Why should they work inside a DOCTYPE definition? "Validate" (eclipse context menu) the log4j.xml file and you will get a warning that the path can not be resolved.

I hoped classpath:org/apache/log4j/xml/log4j.dtd would do the trick but that protocol is also not support (see validation error). I am afraid it will not work out of the box.

And, as I understood, the SYSTEM "log4j.dtd" notation is no placeholder. It is a valid reference to a document that is expected to be found next to the dtd (in this case).

Sridhar
  • 11,466
  • 5
  • 39
  • 43
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • I see what you are saying, but this seems to be an overall property. However, we have different projects (some not even Maven-projects) and they use different versions of log4j. It would be great if I could somewhat bind the configuration to the project. – sjngm Feb 15 '11 at 08:41
  • If you say different versions of log4j - do you mean different versions of log4j.dtd? Than you have to define a Catalog entry for each version. I you want to bind the configuration to the project it might be best to place the log4j.dtd next to the log4j.xml (inside the project) and than SYSTEM "log4j.dtd" should work (I hope). – FrVaBe Feb 15 '11 at 10:21
  • Sorry, I meant different version of the `log4j.jar` as a whole. I assume that "log4j.dtd" is some sort of place-holder as it is such a general name. -- Yes, copying the log4j.dtd next to the XML would be a solution, but is this really a common way to do it? – sjngm Feb 15 '11 at 11:58
  • 1
    And please note that we are talking about a solution here that does not target the main question. – sjngm Feb 15 '11 at 11:59
  • Sorry did not notice that the http reference worked - I think it is the best solution - or pleace given reference for your statement `SYSTEM "log4j.dtd"` _is the way to do it_. – FrVaBe Feb 15 '11 at 20:15
  • Regarding EDIT: I know I said "place-holder", but maybe it was the wrong word to use. However, it seems as if many use it like one. -- Your link to Eclipse's help seems to explain why it works as it works. Let me keep this open for a little longer, maybe anyone else can add something to this... – sjngm Feb 18 '11 at 06:37
  • I know that "does not work" is not the answer gladly to accept :-) Let me say that I am not an expert on this and only found the question intersting enough to spend some work on it. For me I found an acceptabl way to make a valid (http) dtd reference that works in eclipse even if no internet connection is provided. Feel free to let this question open! You are interested on more input on this. So do I! – FrVaBe Feb 18 '11 at 14:34
  • Using `SYSTEM "log4j.dtd"` for me looks like _Just don't care_ - if the document is not provided beside the xml document. – FrVaBe Feb 18 '11 at 14:37
  • Hehe. This won't be the first "doesn't work" answer I have to accept ;) – sjngm Feb 21 '11 at 06:47
2

I added DTD folder in webcontent and then I copied the log4j dtd file in that. then i tried like bellow. Its working

<!DOCTYPE log4j:configuration SYSTEM "<Path>/DTD/log4j.dtd">

Path means here the project path like /projectname

Dileep
  • 21
  • 1
1

I have tried with FrVaBe's answer but didn't work for me and I did an small change in the Key value and it works.

"Preferences -> XML -> XML Catalog"

Localization: C:\Users\me\Desktop\log4j.dtd
Key Type: URI
Key: -//APACHE//DTD LOG4J 1.2//EN
Abel ANEIROS
  • 6,029
  • 2
  • 25
  • 19
1

@Jack Leow uses a good approach with the PUBLIC ID. Yet, as he points out, it requires a network connection.

I prefer a combination:

Entry element:      Public
Location:           org\apache\log4j\xml\log4j.dtd in jar file C:\Development\lib\external\apache-log4j-1.2.17\log4j-1.2.17.jar
URI:                jar:file:/C:/Development/lib/external/apache-log4j-1.2.17/log4j-1.2.17.jar!/org/apache/log4j/xml/log4j.dtd
Key type:           Public ID
Key:                -//APACHE//DTD LOG4J 1.2//EN

This references a local JAR, and supports a DOCTYPE declaration without the full URL.

<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
Community
  • 1
  • 1
wbdarby
  • 1,129
  • 12
  • 12
1

If you place the log4j.dtd at the same location as your log4j.xml, the declaration

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

is good for eclipse (at least eclipse 2020-06).

BTW: The eclipse error does not disappear immediately, but it disappears after doing some edits within the log4j.xml file.

Heri
  • 4,368
  • 1
  • 31
  • 51
0

Usually, Eclipse looks for log4j.dtd in classpath and it doesn’t find it there and hence the error. We can resolve this issue by providing URL for log4j.dtd file like below.

<!DOCTYPE log4j:configuration SYSTEM 
      "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
Arnaud
  • 7,259
  • 10
  • 50
  • 71
Nirbhay Rana
  • 4,229
  • 2
  • 18
  • 4