问题描述
在我的 Tapestry 应用程序中,我需要在某个时间点向某个 URL 发出 GET 请求.我这样做(使用 Apache 的 HttpClient 4.2.1)
String scheme = getScheme(); String host = getHost(); int port = getPort(); String path = getPath(); String paramKey = getParamKey(); String paramValue = getParamValue(); URIBuilder builder = new URIBuilder(); builder .setScheme(scheme) .setHost(host) .setPort(port) .setPath(path) .setParameter(paramKey, paramValue); URI uri = builder.build(); HttpGet getRequest = new HttpGet(uri); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(getRequest);
当我在 Glassfish (3.1.2.2) 上部署我的 WAR 并执行相关代码时,会引发以下异常:
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@78aded17 for org.apache.commons.logging.impl.Log4JLogger (Caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category) (Caused by org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@78aded17 for org.apache.commons.logging.impl.Log4JLogger (Caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category)) at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351) at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187) at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)
我在我的 pom.xml 中指定了这个
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency>
我检查并确保"未找到"的类实际上在 WEB-INF/lib/log4j-1.2.14 中.
有没有办法解决这个问题?
推荐答案
你有一个类加载器问题.可能会先加载另一个使用不同 log4j 版本的 Web 应用程序.类加载器使用它找到的类的第一个版本.试试这个:
- 从应用服务器中删除所有其他应用程序
- 检查服务器的类路径中是否没有另一个 log4j.
尝试安装并启动您的应用程序.
问题描述
In my Tapestry application, I need to do a GET request to a certain URL at some point. I do it like this (using Apache's HttpClient 4.2.1)
String scheme = getScheme(); String host = getHost(); int port = getPort(); String path = getPath(); String paramKey = getParamKey(); String paramValue = getParamValue(); URIBuilder builder = new URIBuilder(); builder .setScheme(scheme) .setHost(host) .setPort(port) .setPath(path) .setParameter(paramKey, paramValue); URI uri = builder.build(); HttpGet getRequest = new HttpGet(uri); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(getRequest);
When I deploy my WAR on Glassfish (3.1.2.2) and the code in question is executed, the following exception is thrown:
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@78aded17 for org.apache.commons.logging.impl.Log4JLogger (Caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category) (Caused by org.apache.commons.logging.LogConfigurationException: No suitable Log constructor [Ljava.lang.Class;@78aded17 for org.apache.commons.logging.impl.Log4JLogger (Caused by java.lang.NoClassDefFoundError: org/apache/log4j/Category)) at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351) at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187) at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)
I specify this in my pom.xml
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency>
And I checked and made sure the Class that is "not being found" is actually in WEB-INF/lib/log4j-1.2.14.
Is there any way to solve this issue?
推荐答案
You have a classloader problem. Maybe another web application, that uses different log4j version, is loaded first. The classloader uses the first version of the class it finds. Try this:
- Remove all other applications from app server
- Check that you don't have another log4j in classpath of the server.
Try to install and start your application.