java - 错误:无法初始化主类 Lucene_workspace.Class_Indexer
问题描述
我是编程世界的新手,所以我尝试使用 Lucene 索引一个包含 3204 个文档的 XML 文件,但我收到此错误:错误:无法初始化主类 Lucene_workspace.Class_Indexer 原因:java.lang.NoClassDefFoundError : org/apache/lucene/index/IndexableField 我试图修复它但没有任何效果。这是代码和 XML 文件。
package Lucene_workspace;
import java.io.IOException;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Class_Indexer{
public static void main (String[] args) throws IOException
{
Class_Indexer test = new Class_Indexer();
test.readXML();
}
private void readXML() throws IOException
{
Document doc = null;
try
{
doc = parseXML("C:\\Users\\pc\\Desktop\\projet RI\\BIB_DATA\\cacm.xml");
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
if(doc != null)
{
NodeList nList = doc.getElementsByTagName("document");
for (int i = 0; i < nList.getLength(); i++)
{
org.apache.lucene.document.Document document = new org.apache.lucene.document.Document();
String titleValue = doc.getElementsByTagName("title").item(i).getTextContent();
Field title = new TextField("title", titleValue, Field.Store.YES);
document.add(title);
System.out.println("title "+i+" : "+ titleValue);
StandardAnalyzer analyzer = new StandardAnalyzer();
Directory directory = FSDirectory.open(Paths.get("C:\\Users\\pc\\Desktop\\projet RI\\BIB_INDEX"));
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter iwriter = new IndexWriter(directory, config);
iwriter.addDocument(document);
iwriter.close();
}
}
}
private Document parseXML(String filePath) throws ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(filePath);
doc.getDocumentElement().normalize();
return doc;
}
}
解决方案
推荐阅读
- python-3.x - matplotlib 仅显示来自 stl 文件的部分网格
- java - 如何使用 Java Api 在 Bamboo 中获取当前分支名称
- javascript - 材料化进行电子邮件验证的 js 代码
- associations - CakePHP3 PUT 与关联实体
- javascript - 函数中的 JavaScript 函数 - TypeError:this.outputName 不是函数
- python - 从文件(文本)(如 python 等)执行 CURL 脚本(外部)
- java - 重启后广播接收器不工作
- javascript - 浏览器文本编辑器
- javascript - 在导航守卫中将 next() 放入 Promise 时发出警告
- ios - 在 UITableViewCell 中逐行显示字符串的动画