【问题标题】:No class definition found error occurs while trying to run the lucene app from command line尝试从命令行运行 lucene 应用程序时发生未找到类定义错误
【发布时间】:2012-06-06 16:12:38
【问题描述】:

我是 lucene 的初学者,我曾尝试运行网站 http://www.lucenetutorial.com/lucene-in-5-minutes.html 中提供的示例应用程序 (HelloLucene.java)。代码已成功编译,并为应用程序创建了一个类文件 (HelloLucene.class)。

但是在尝试运行应用程序时,出现以下错误

  • 无法找到或加载主类 HelloLucene

我已经参考了链接Java program uses or overrides a deprecated API? 并尝试了提供的解决方案,但都是徒劳的。请帮我解决这个问题。

我用来运行代码的命令是

  • java HelloLucene lucene

我尝试运行的代码:

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.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;
import org.apache.lucene.util.Version;

import java.io.IOException;

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(Version.LUCENE_35);

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

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);

IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action");
addDoc(w, "Lucene for Dummies");
addDoc(w, "Managing Gigabytes");
addDoc(w, "The Art of Computer Science");
w.close();

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

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

// 3. search
int hitsPerPage = 10;
IndexReader reader = IndexReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
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("title"));
}

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

 private static void addDoc(IndexWriter w, String value) throws IOException {
   Document doc = new Document();
   doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
   w.addDocument(doc);
 }
}

【问题讨论】:

  • 嗨 Rajesh,请使用您用于运行代码的命令更新问题。鉴于您成功编译了代码,根本原因可能就在那里。
  • 谢谢你,Rob。我将使用我用来运行代码的命令进行更新。

标签: lucene runtime-error classnotfound


【解决方案1】:

看起来您需要设置类路径,以包括 Lucene JAR 文件和包含 HelloLucene 类的目录。整个命令应该是这样的:

java -classpath .:lucene-core-3.4.0.jar HelloLucene lucene

【讨论】:

  • 谢谢Kai Chan。我已经将classpath设置为系统变量,所以没有明确提及。虽然我试过上面的命令,但同样的错误来了
猜你喜欢
  • 2021-09-24
  • 2016-04-04
  • 2017-04-08
  • 2020-02-13
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
相关资源
最近更新 更多