错误:无法初始化主类test.helloluce-原因:java.lang.NoClassDefFoundError:org/apache/lucene/analysis/Analyzer

错误:无法初始化主类test.helloluce-原因:java.lang.NoClassDefFoundError:org/apache/lucene/analysis/Analyzer,java,lucene,Java,Lucene,我是Java开发新手,刚开始使用Lucene。我试图开始使用下面给出的示例代码和以下导入,但我一直在标题中得到错误。还尝试了Lucene的旧版本,结果相同。 尝试从库中导入core和queryparser jar,但没有成功。欢迎提出任何建议 import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Docum

我是Java开发新手,刚开始使用Lucene。我试图开始使用下面给出的示例代码和以下导入,但我一直在标题中得到错误。还尝试了Lucene的旧版本,结果相同。 尝试从库中导入core和queryparser jar,但没有成功。欢迎提出任何建议

import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

public class HelloLucene {
public static void main(String[] args) throws IOException, ParseException {
    // 0. Specify the analyzer for tokenizing text.
    //    The same analyzer should be used for indexing and searching
    StandardAnalyzer analyzer = new StandardAnalyzer();

    // 1. create the index
    Directory index = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(analyzer);

    IndexWriter w = new IndexWriter(index, config);
    addDoc(w, "Lucene in Action", "193398817");
    addDoc(w, "Lucene for Dummies", "55320055Z");
    addDoc(w, "Managing Gigabytes", "55063554A");
    addDoc(w, "The Art of Computer Science", "9900333X");
    w.close();

    // 2. query
    String querystr = args.length > 0 ? args[0] : "action";

    // the "title" arg specifies the default field to use
    // when no field is explicitly specified in the query.
    Query q = new QueryParser("title", analyzer).parse(querystr);

    // 3. search
    int hitsPerPage = 10;
    //int hitsTotal = 100000;
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
    searcher.search(q, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    
    // 4. display results
    System.out.println("Found " + hits.length + " hits.");
    for(int i=0;i<hits.length;++i) {
      int docId = hits[i].doc;
      Document d = searcher.doc(docId);
      System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title"));
    }

    // reader can only be closed when there
    // is no need to access the documents any more.
    reader.close();
  }

  private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
    Document doc = new Document();
    doc.add(new TextField("title", title, Field.Store.YES));

    // use a string field for isbn because we don't want it tokenized
    doc.add(new StringField("isbn", isbn, Field.Store.YES));
    w.addDocument(doc);
  }
}
import java.io.IOException;
导入org.apache.lucene.analysis.standard.StandardAnalyzer;
导入org.apache.lucene.document.document;
导入org.apache.lucene.document.Field;
导入org.apache.lucene.document.StringField;
导入org.apache.lucene.document.TextField;
导入org.apache.lucene.index.DirectoryReader;
导入org.apache.lucene.index.IndexReader;
导入org.apache.lucene.index.IndexWriter;
导入org.apache.lucene.index.IndexWriterConfig;
导入org.apache.lucene.queryparser.classic.ParseException;
导入org.apache.lucene.queryparser.classic.queryparser;
导入org.apache.lucene.search.indexsearch;
导入org.apache.lucene.search.Query;
导入org.apache.lucene.search.ScoreDoc;
导入org.apache.lucene.search.TopScoreDocCollector;
导入org.apache.lucene.store.Directory;
导入org.apache.lucene.store.RAMDirectory;
公共级HelloLucene{
公共静态void main(字符串[]args)引发IOException、ParseException{
//0.指定用于标记文本的分析器。
//应使用相同的分析器进行索引和搜索
StandardAnalyzer=新的StandardAnalyzer();
//1.创建索引
目录索引=新的RAMDirectory();
IndexWriterConfig配置=新的IndexWriterConfig(分析器);
IndexWriter w=新的IndexWriter(索引,配置);
addDoc(w,“Lucene在行动”,“193398817”);
addDoc(w,“用于假人的Lucene”,“55320055Z”);
addDoc(w,“管理千兆字节”,“55063554A”);
addDoc(w,“计算机科学的艺术”,“9900333X”);
w、 close();
//2.查询
字符串querystr=args.length>0?args[0]:“操作”;
//“title”参数指定要使用的默认字段
//当查询中未显式指定字段时。
Query q=新的QueryParser(“title”,analyzer).parse(querystr);
//3.搜索
int hitsPerPage=10;
//int hitsTotal=100000;
IndexReader=DirectoryReader.open(索引);
IndexSearcher search=新的IndexSearcher(阅读器);
TopScoreDocCollector=TopScoreDocCollector.create(hitsPerPage);
搜索者。搜索(q,收集器);
ScoreDoc[]hits=collector.topDocs().scoreDocs;
//4.显示结果
System.out.println(“Found”+hits.length+“hits.”);

对于(int i=0;i您的项目似乎缺少Lucene库(JAR文件)它需要。如果您使用的是Maven,则可以将它们作为依赖项添加。您需要和。根据编写代码示例的Lucene版本,您可能需要更改版本,或者更新代码以说明API的更改。如果您手动包含JAR(不推荐)您可能还需要包括
lucene查询
lucene沙盒
的jar。但是如果您使用的是Maven(或Gradle),您应该根据我第一条评论中的注释自动获得任何额外的jar。之后,您需要澄清您的意思“试图从库中导入core和queryparser JAR,但运气不佳。”,因为这太模糊了,我们无法在不进行猜测的情况下帮助您。