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

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

compile java with external jar file in terminal

问题

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

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

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

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

但是我收到了以下错误:

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

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

这是 IndexFiles 类的内容:

  1. package lucene;
  2. import org.apache.lucene.analysis.Analyzer;
  3. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  4. import org.apache.lucene.document.*;
  5. import org.apache.lucene.index.IndexWriter;
  6. import org.apache.lucene.index.IndexWriterConfig;
  7. import org.apache.lucene.index.IndexWriterConfig.OpenMode;
  8. import org.apache.lucene.index.Term;
  9. import org.apache.lucene.store.Directory;
  10. import org.apache.lucene.store.FSDirectory;
  11. import java.io.FileReader;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.nio.charset.Charset;
  15. import java.nio.charset.StandardCharsets;
  16. import java.nio.file.*;
  17. import java.nio.file.attribute.BasicFileAttributes;
  18. import java.util.Date;
  19. /** 对目录下的所有文本文件进行索引。
  20. * <p>
  21. * 这是一个命令行应用程序,演示了简单的Lucene索引操作。
  22. * 在没有命令行参数的情况下运行,以获取使用信息。
  23. */
  24. public class IndexFiles {
  25. ....
英文:

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

  1. 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:

  1. 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:

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

what should I do? and what is my wrong?

and this is IndexFiles class

  1. package lucene;
  2. import org.apache.lucene.analysis.Analyzer;
  3. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  4. import org.apache.lucene.document.*;
  5. import org.apache.lucene.index.IndexWriter;
  6. import org.apache.lucene.index.IndexWriterConfig;
  7. import org.apache.lucene.index.IndexWriterConfig.OpenMode;
  8. import org.apache.lucene.index.Term;
  9. import org.apache.lucene.store.Directory;
  10. import org.apache.lucene.store.FSDirectory;
  11. import java.io.FileReader;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.nio.charset.Charset;
  15. import java.nio.charset.StandardCharsets;
  16. import java.nio.file.*;
  17. import java.nio.file.attribute.BasicFileAttributes;
  18. import java.util.Date;
  19. /** Index all text files under a directory.
  20. * <p>
  21. * This is a command-line application demonstrating simple Lucene indexing.
  22. * Run it with no command-line arguments for usage information.
  23. */
  24. public class IndexFiles {
  25. ....

答案1

得分: 0

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

  1. 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
  2. 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.

  1. 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
  2. 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:

确定