在终端中使用外部jar文件编译java
嗨,我想在终端中编译java代码,我在终端中输入下面的命令在终端中使用外部jar文件编译java,java,compilation,external-library,Java,Compilation,External Library,嗨,我想在终端中编译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.
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
但我得到了这个错误:
Error: Could not find or load main class IndexFiles
Caused by: java.lang.NoClassDefFoundError: lucene/IndexFiles (wrong name: 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;
/** 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 {
....
封装lucene;
导入org.apache.lucene.analysis.Analyzer;
导入org.apache.lucene.analysis.standard.StandardAnalyzer;
导入org.apache.lucene.document.*;
导入org.apache.lucene.index.IndexWriter;
导入org.apache.lucene.index.IndexWriterConfig;
导入org.apache.lucene.index.IndexWriterConfig.OpenMode;
导入org.apache.lucene.index.Term;
导入org.apache.lucene.store.Directory;
导入org.apache.lucene.store.FSDirectory;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.nio.charset.charset;
导入java.nio.charset.StandardCharset;
导入java.nio.file.*;
导入java.nio.file.attribute.BasicFileAttributes;
导入java.util.Date;
/**为目录下的所有文本文件编制索引。
*
*这是一个命令行应用程序,演示了简单的Lucene索引。
*运行它时,不要使用命令行参数获取使用信息。
*/
公共类索引文件{
....
由于您的类位于包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
谢谢您Konrand Rudilph我编辑了我的问题是否可以为我输入的终端编写完整的命令我不是java专业人士?thanks@fahimjafari好的,命令现在已完成。这是两个单独的命令,您可以在命令行上依次输入错误更改为:错误:无法初始化主类lucene.IndexFiles,原因是:java.lang.NoClassDefFoundError:org/apache/lucene/analysis/analyzer。当我在intellij中运行此项目时,它会运行,但我需要在terminal中运行此命令谢谢康拉德·鲁道夫我接受了你的回答,但是如果你知道我该怎么处理这个新错误,请帮助我/\