1

Hi I want to compile java code in the terminal I enter the below command in terminal

javac -cp .:../../lucene-8.6.3/lucene-8.6.3/core/lucene-core-8.6.3.jar:../../lucene-8.6.3/lucene-8.6.3/analysis/common/lucene-analyzers-common-8.6.3.jar:../../lucene-8.6.3/lucene-8.6.3/queryparser/lucene-queryparser-8.6.3.jar IndexFiles.java

and i dont get any error, and after i enter below command to run:

java -cp .:../../lucene-8.6.3/lucene-8.6.3/core/lucene-core-8.6.3.jar:../../lucene-8.6.3/lucene-8.6.3/analysis/common/lucene-analyzers-common-8.6.3.jar:../../lucene-8.6.3/lucene-8.6.3/queryparser/lucene-queryparser-8.6.3.jar IndexFiles

but I get this error:

Error: Could not find or load main class IndexFiles
Caused by: java.lang.NoClassDefFoundError: lucene/IndexFiles (wrong name: IndexFiles)

what should I do? and what is my wrong?

and this is IndexFiles class

package lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;

/** Index all text files under a directory.
 * <p>
 * This is a command-line application demonstrating simple Lucene indexing.
 * Run it with no command-line arguments for usage information.
 */
public class IndexFiles {
    ....
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

Since your class is in the package lucene, that’s the class name that you need to pass to java: it requires the fully qualified name.

CLASSPATH=../../lucene-8.6.3/lucene-8.6.3/core/lucene-core-8.6.3.jar:../../lucene-8.6.3/lucene-8.6.3/analysis/common/lucene-analyzers-common-8.6.3.jar:../../lucene-8.6.3/lucene-8.6.3/queryparser/lucene-queryparser-8.6.3.jar
java -cp ".:$CLASSPATH" lucene.IndexFiles

You’ll also need to make sure that the .class file is actually at the right location. In your case, that would be ./lucene/IndexFiles.class.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214