2014-09-02 58 views
2

我正在通过扩展SearchComponent类来编写solr插件。我的新课程是a.jar归档的一部分。我的课也取决于jar b.jar。我将两个jar放在我的core/lib文件夹中,然后在solrconfig.xml中声明我的新组件。该组件被正确调用,直到b.jar中的类ClassFromB尝试从classpath加载类路径资源stopwords.txt。Solr插件类加载器

的一块类ClassFromB代码如下所示:

... 
ClassLoader cl = Thread.currentThread().getContextClassLoader(); 
URL url = cl.getResource("stopwords.txt"); //with cl.getResource("/stopwords.txt"); I get the same exception 
String content = IOUtils.toString(curl.openConnection().getInputStream()); 
... 

上面最后一行抛出一个NPE告诉我,资源没有找到。 因此,类ClassFromB被加载,因为它执行代码直到失败的最后一行。此外,我再次检查b.jar,并可以在根目录中查看stopwords.txt文件。

我该怎么做才能从类ClassFromB中看到该资源?

+0

如何包命名,其中stopWords.txt中居住?您是否尝试用“绝对路径”来解决文件? – cheffe 2014-09-05 11:17:06

+0

该类的FQN是com.sxxxx.solr.ClassFromB。文件synonyms synonyms .txt文件位于默认软件包中(位于b.jar文件夹中)。 – biliboc 2014-09-05 15:17:56

+0

您是否找到解决这个老问题的方法? – Klaus 2016-03-29 20:51:54

回答

0

然而,这工作:

public final class Stopwords { 

    static { 
     URL curl = Stopwords.class.getClassLoader().getResource("stopwords.txt"); 
     .... 
    } 
    .... 
}