使用终端编译带有外部 JAR 文件的 Java。

huangapple go评论80阅读模式
英文:

compile java with external jar file in terminal

问题

你好,我想在终端中编译Java代码,我在终端中输入以下命令:

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

我没有收到任何错误,然后我输入以下命令来运行:

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

但是我收到了以下错误:

错误: 找不到或无法加载主类 IndexFiles
原因: java.lang.NoClassDefFoundError: lucene/IndexFiles (名称不正确: IndexFiles)

我该怎么办?我错在哪里?

这是 IndexFiles 类的内容:

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;

/** 对目录下的所有文本文件进行索引。
 * <p>
 * 这是一个命令行应用程序,演示了简单的Lucene索引操作。
 * 在没有命令行参数的情况下运行,以获取使用信息。
 */
public class IndexFiles {
    ....
英文:

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 {
    ....

答案1

得分: 0

由于你的类位于lucene包中,所以这个类名是你需要传递给java的,它需要完全限定名

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

你还需要确保.class文件实际上位于正确的位置。在你的情况下,路径应为./lucene/IndexFiles.class

英文:

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.

huangapple
  • 本文由 发表于 2020年10月17日 20:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64402533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定