首页 > 解决方案 > 错误:无法初始化主类 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;
}

}

标签: javaxmlindexingxml-parsinglucene

解决方案


推荐阅读