Eclipse:在 log4j.xml 中引用 log4j.dtd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5000884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse: Referencing log4j.dtd in log4j.xml
提问by sjngm
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 theway to do it):
我已经使用 log4j 有一段时间了,我通常在 log4j.xml 的顶部使用它(可能就像许多其他人一样,根据谷歌,这是这样做的方法):
<!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.
显然这是有效的,但是 Eclipse 并没有提供用于编写 XML 和所有内容的上下文相关帮助。此外,它总是显示一个警告,它没有找到log4j.dtd. 现在我很好奇如何解决这个问题。
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:
正如您从上面看到的,我们正在使用 Maven。因此,我尝试了这个,但失败了:
<!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.
Eclipse 通常知道如何处理类路径变量,但是为什么这不起作用?我知道引用在运行时不起作用,但简单的log4j.dtd(如果我没有错的话)也不起作用,所以这应该不是问题。
Can anyone please shed a light on this?
任何人都可以对此有所了解吗?
回答by Hyman Leow
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).
它类似于@FrVaBe 的 response,但从好的方面来说,不需要任何进一步的 Eclipse 配置(即,如果您与他人共享您的项目,或者拥有一个大型团队,则无需担心)。
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).
但不利的一面是,我相信这意味着您将需要 Internet 连接(至少在开发过程中的某个时刻,即使只是一次)。
回答by FrVaBe
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.
尝试将 log4j.dtd 添加为“首选项 -> XML -> XML 目录”中的用户特定 URI XML 目录条目。据我所知,这是 eclipse 管理对定义/验证文件(如 xsd)的引用的地方。如果可以在此处找到它们,则 eclipse 无需互联网访问即可在其本地(网络)位置访问它们。
I did it like this (for test) and eclipse does not complain:
我是这样做的(为了测试)并且 eclipse 没有抱怨:
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.
也许 ${M2_REPO} 也有效 - 我没有检查这个。
Use the native URL in your log4j.xml afterwards
之后在 log4j.xml 中使用本机 URL
<!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.
我会采用上述解决方案,但回到您的问题,我认为类路径变量'can be used in a Java Build Path'。为什么他们应该在 DOCTYPE 定义中工作?“验证”(eclipse 上下文菜单)log4j.xml 文件,您将收到一条警告,指出无法解析路径。
I hoped classpath:org/apache/log4j/xml/log4j.dtdwould do the trick but that protocol is also not support (see validation error). I am afraid it will not work out of the box.
我希望classpath:org/apache/log4j/xml/log4j.dtd能做到这一点,但该协议也不支持(请参阅验证错误)。恐怕它不会开箱即用。
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).
而且,据我所知,该SYSTEM "log4j.dtd"符号不是占位符。它是对预期在 dtd 旁边找到的文档的有效引用(在本例中)。
回答by Dileep
I added DTD folder in webcontent and then I copied the log4j dtd file in that. then i tried like bellow. Its working
我在 webcontent 中添加了 DTD 文件夹,然后在其中复制了 log4j dtd 文件。然后我尝试如下。它的工作
<!DOCTYPE log4j:configuration SYSTEM "<Path>/DTD/log4j.dtd">
Path means here the project path like /projectname
路径在这里表示项目路径,如 /projectname
回答by Abel ANEIROS
I have tried with FrVaBe's answer but didn't work for me and I did an small change in the Keyvalue and it works.
我曾尝试使用 FrVaBe 的答案,但对我不起作用,我对Key值做了一个小改动,它起作用了。
"Preferences -> XML -> XML Catalog"
“首选项 -> XML -> XML 目录”
Localization: C:\Users\me\Desktop\log4j.dtd
Key Type: URI
Key: -//APACHE//DTD LOG4J 1.2//EN
回答by wbdarby
@Hyman Leowuses a good approach with the PUBLIC ID. Yet, as he points out, it requires a network connection.
@Hyman Leow使用 PUBLIC ID 的好方法。然而,正如他指出的那样,它需要网络连接。
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.
这引用了一个本地 JAR,并支持没有完整 URL 的 DOCTYPE 声明。
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
回答by Nirbhay Rana
Usually, Eclipse looks for log4j.dtdin classpath and it doesn't find it there and hence the error. We can resolve this issue by providing URL for log4j.dtdfile like below.
通常,Eclipselog4j.dtd在类路径中查找,但在那里找不到它,因此会出现错误。我们可以通过提供log4j.dtd如下文件的URL 来解决此问题。
<!DOCTYPE log4j:configuration SYSTEM
"http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

