使用Solrj 5.2.1和Maven

时间:2015-07-21 20:06:28

标签: maven solrj

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

1 个答案:

答案 0 :(得分:1)

Solrj依赖于两个通用日志记录库:SLF4J和Commons Logging。每个人都需要某种真正的实现才能工作。将以下依赖项(Commons Logging to SLF4J重定向和基于控制台的简单appender)添加到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>