将 Solrj 5.2.1 与 Maven 结合使用
Using Solrj 5.2.1 with Maven
我创建了一个具有 solrj 依赖项的新 Maven 项目:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.2.1</version>
</dependency>
它编译正确,但如果您尝试使用一些 solrj 代码,应用程序会崩溃,但出现以下异常:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.impl.client.CloseableHttpClient.<init>(CloseableHttpClient.java:58)
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:287)
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:128)
at org.apache.http.impl.client.SystemDefaultHttpClient.<init>(SystemDefaultHttpClient.java:116)
at org.apache.solr.client.solrj.impl.HttpClientUtil.createClient(HttpClientUtil.java:117)
at org.apache.solr.client.solrj.impl.CloudSolrClient.<init>(CloudSolrClient.java:189)
at Main.main(Main.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 more
Solrj 依赖于两个通用日志记录库:SLF4J 和 Commons Logging。每个都需要某种真正的实现才能工作。将以下依赖项(Commons Logging 到 SLF4J 重定向和简单的基于控制台的附加程序)添加到 pom.xml 解决了这个问题。
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.7</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.7</version>
</dependency>
我创建了一个具有 solrj 依赖项的新 Maven 项目:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.2.1</version>
</dependency>
它编译正确,但如果您尝试使用一些 solrj 代码,应用程序会崩溃,但出现以下异常:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.impl.client.CloseableHttpClient.<init>(CloseableHttpClient.java:58)
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:287)
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:128)
at org.apache.http.impl.client.SystemDefaultHttpClient.<init>(SystemDefaultHttpClient.java:116)
at org.apache.solr.client.solrj.impl.HttpClientUtil.createClient(HttpClientUtil.java:117)
at org.apache.solr.client.solrj.impl.CloudSolrClient.<init>(CloudSolrClient.java:189)
at Main.main(Main.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 more
Solrj 依赖于两个通用日志记录库:SLF4J 和 Commons Logging。每个都需要某种真正的实现才能工作。将以下依赖项(Commons Logging 到 SLF4J 重定向和简单的基于控制台的附加程序)添加到 pom.xml 解决了这个问题。
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.7</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.7</version>
</dependency>