Eclipse:引用log4j。dtd在log4j.xml

时间:2021-09-20 21:54:09

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):

我使用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?

有人能告诉我这个吗?

6 个解决方案

#1


157  

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的响应,但是在好的方面,不需要任何进一步的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).

尽管如此,我认为这意味着你需要一个互联网连接(至少在开发过程中,哪怕只是一次)。

#2


37  

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。在“首选项-> XML -> XML目录”中,dtd作为用户特定的URI XML目录项。正如我所知道的,这是eclipse管理对定义/验证文件(如xsd)的引用的地方。如果可以在这里找到它们,eclipse就不需要通过internet访问它们的本地(web)位置。

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中使用本机URL。xml之后

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

我将使用上面的解决方案,但回到您的问题,我认为类路径变量可以在Java构建路径中使用。为什么要在DOCTYPE定义中工作?“验证”(eclipse上下文菜单)log4j。xml文件和您将得到一条路径无法解决的警告。

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.

我希望类路径: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).

正如我所理解的,系统“log4j”。dtd“符号不是占位符。它是对将要在dtd(在本例中)找到的文档的有效引用。

#3


2  

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)。

#4


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.

我尝试了FrVaBe的答案,但对我不起作用,我在键值上做了一个小小的改变,它起作用了。

"Preferences -> XML -> XML Catalog"

“首选项-> XML -> XML目录”

Localization: C:\Users\me\Desktop\log4j.dtd
Key Type: URI
Key: -//APACHE//DTD LOG4J 1.2//EN

#5


1  

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

@Jack Leow使用了一种很好的公共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">

#6


-1  

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.

通常,Eclipse会查找log4j。类路径中的dtd并没有发现它,因此出现了错误。我们可以通过为log4j提供URL来解决这个问题。下面的dtd文件等。

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

#1


157  

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的响应,但是在好的方面,不需要任何进一步的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).

尽管如此,我认为这意味着你需要一个互联网连接(至少在开发过程中,哪怕只是一次)。

#2


37  

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。在“首选项-> XML -> XML目录”中,dtd作为用户特定的URI XML目录项。正如我所知道的,这是eclipse管理对定义/验证文件(如xsd)的引用的地方。如果可以在这里找到它们,eclipse就不需要通过internet访问它们的本地(web)位置。

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中使用本机URL。xml之后

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

我将使用上面的解决方案,但回到您的问题,我认为类路径变量可以在Java构建路径中使用。为什么要在DOCTYPE定义中工作?“验证”(eclipse上下文菜单)log4j。xml文件和您将得到一条路径无法解决的警告。

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.

我希望类路径: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).

正如我所理解的,系统“log4j”。dtd“符号不是占位符。它是对将要在dtd(在本例中)找到的文档的有效引用。

#3


2  

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)。

#4


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.

我尝试了FrVaBe的答案,但对我不起作用,我在键值上做了一个小小的改变,它起作用了。

"Preferences -> XML -> XML Catalog"

“首选项-> XML -> XML目录”

Localization: C:\Users\me\Desktop\log4j.dtd
Key Type: URI
Key: -//APACHE//DTD LOG4J 1.2//EN

#5


1  

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

@Jack Leow使用了一种很好的公共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">

#6


-1  

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.

通常,Eclipse会查找log4j。类路径中的dtd并没有发现它,因此出现了错误。我们可以通过为log4j提供URL来解决这个问题。下面的dtd文件等。

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