java - 从命令行错误在 Linux 上编译 Java 应用程序

标签 java linux compilation

我是一个 Java 新手,事实上我使用它的唯一原因是因为 lucene。

我在Windows上使用eclipse开发了一个lucene应用程序并工作。所以现在当我尝试从 Linux 命令行编译应用程序时,它不起作用。

这是我的文件夹结构。

search_example/
├── lucene-5.0.0
│   ├── analysis
│   │   └── common
│   │       └── lucene-analyzers-common-5.0.0.jar
│   ├── core
│   │   └── lucene-core-5.0.0.jar
│   ├── queries
│   │   └── lucene-queries-5.0.0.jar
│   └── queryparser
│       └── lucene-queryparser-5.0.0.jar
├── lucene_test
│   ├── IndexFiles.java
│   ├── IndexMarkdown.java
│   ├── SearchFiles.java
│   └── SearchMarkdown.java
└── readme.md  

这里是编译调用:

$ cd ~/search_example
$ javac lucene_test/IndexMarkdown.java -classpath ./; lucene-5.0.0/analysis/common/lucene-analyzers-common-5.0.0.jar; lucene-5.0.0/core/lucene-core-5.0.0.jar; lucene-5.0.0/queries/lucene-queries-5.0.0.jar ; lucene-5.0.0/queryparser/lucene-queryparser-5.0.0.jar

我收到以下错误,表明它找不到我作为“类路径”传入的“jar”文件

./lucene_test/IndexFiles.java:20: error: package org.apache.lucene.analysis does not exist
import org.apache.lucene.analysis.Analyzer;
                                 ^
./lucene_test/IndexFiles.java:21: error: package org.apache.lucene.analysis.standard does not exist
import org.apache.lucene.analysis.standard.StandardAnalyzer;
                                          ^
./lucene_test/IndexFiles.java:22: error: package org.apache.lucene.document does not exist
import org.apache.lucene.document.Document;
                                 ^
./lucene_test/IndexFiles.java:23: error: package org.apache.lucene.document does not exist
import org.apache.lucene.document.Field;
                                 ^
./lucene_test/IndexFiles.java:24: error: package org.apache.lucene.document does not exist
import org.apache.lucene.document.LongField;
                                 ^
./lucene_test/IndexFiles.java:25: error: package org.apache.lucene.document does not exist
import org.apache.lucene.document.StringField;
                                 ^
./lucene_test/IndexFiles.java:26: error: package org.apache.lucene.document does not exist
import org.apache.lucene.document.TextField;
                                 ^
./lucene_test/IndexFiles.java:27: error: package org.apache.lucene.index does not exist
import org.apache.lucene.index.IndexWriter;
                              ^
./lucene_test/IndexFiles.java:28: error: package org.apache.lucene.index.IndexWriterConfig does not exist
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
                                                ^
./lucene_test/IndexFiles.java:29: error: package org.apache.lucene.index does not exist
import org.apache.lucene.index.IndexWriterConfig;
                              ^
./lucene_test/IndexFiles.java:30: error: package org.apache.lucene.index does not exist
import org.apache.lucene.index.Term;
                              ^
./lucene_test/IndexFiles.java:31: error: package org.apache.lucene.store does not exist
import org.apache.lucene.store.Directory;
                              ^
./lucene_test/IndexFiles.java:32: error: package org.apache.lucene.store does not exist
import org.apache.lucene.store.FSDirectory;
                              ^
./lucene_test/IndexFiles.java:52: error: cannot find symbol
    private IndexWriter _writer;
            ^
  symbol:   class IndexWriter
  location: class IndexFiles
./lucene_test/IndexFiles.java:73: error: cannot find symbol
            Directory dir = FSDirectory.open(_indexPathObj);
            ^
  symbol:   class Directory
  location: class IndexFiles
./lucene_test/IndexFiles.java:73: error: cannot find symbol
            Directory dir = FSDirectory.open(_indexPathObj);
                            ^
  symbol:   variable FSDirectory
  location: class IndexFiles
./lucene_test/IndexFiles.java:74: error: cannot find symbol
            Analyzer analyzer = new StandardAnalyzer();
            ^
  symbol:   class Analyzer
  location: class IndexFiles
./lucene_test/IndexFiles.java:74: error: cannot find symbol
            Analyzer analyzer = new StandardAnalyzer();
                                    ^
  symbol:   class StandardAnalyzer
  location: class IndexFiles
./lucene_test/IndexFiles.java:75: error: cannot find symbol
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
            ^
  symbol:   class IndexWriterConfig
  location: class IndexFiles
./lucene_test/IndexFiles.java:75: error: cannot find symbol
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
                                        ^
  symbol:   class IndexWriterConfig
  location: class IndexFiles
./lucene_test/IndexFiles.java:76: error: cannot find symbol
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
                            ^
  symbol:   variable OpenMode
  location: class IndexFiles
./lucene_test/IndexFiles.java:77: error: cannot find symbol
            _writer = new IndexWriter(dir, iwc);
                          ^
  symbol:   class IndexWriter
  location: class IndexFiles
./lucene_test/IndexFiles.java:161: error: cannot find symbol
      Document doc = new Document();
      ^
  symbol:   class Document
  location: class IndexFiles
./lucene_test/IndexFiles.java:161: error: cannot find symbol
      Document doc = new Document();
                         ^
  symbol:   class Document
  location: class IndexFiles
./lucene_test/IndexFiles.java:164: error: cannot find symbol
      doc.add(new TextField("category", category, Field.Store.YES));
                  ^
  symbol:   class TextField
  location: class IndexFiles
./lucene_test/IndexFiles.java:164: error: package Field does not exist
      doc.add(new TextField("category", category, Field.Store.YES));
                                                       ^
./lucene_test/IndexFiles.java:165: error: cannot find symbol
      doc.add(new StringField("subject", subject, Field.Store.YES));
                  ^
  symbol:   class StringField
  location: class IndexFiles
./lucene_test/IndexFiles.java:165: error: package Field does not exist
      doc.add(new StringField("subject", subject, Field.Store.YES));
                                                       ^
./lucene_test/IndexFiles.java:175: error: cannot find symbol
      doc.add(new LongField("modified", lastModified, Field.Store.NO));
                  ^
  symbol:   class LongField
  location: class IndexFiles
./lucene_test/IndexFiles.java:175: error: package Field does not exist
      doc.add(new LongField("modified", lastModified, Field.Store.NO));
                                                           ^
./lucene_test/IndexFiles.java:181: error: cannot find symbol
      doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
                  ^
  symbol:   class TextField
  location: class IndexFiles
./lucene_test/IndexFiles.java:184: error: cannot find symbol
      if (_writer.getConfig().getOpenMode() == OpenMode.CREATE) 
                                               ^
  symbol:   variable OpenMode
  location: class IndexFiles
./lucene_test/IndexFiles.java:195: error: cannot find symbol
        _writer.updateDocument(new Term("path", file.toString()), doc);
                                   ^
  symbol:   class Term
  location: class IndexFiles

更新:

因为我是在 Linux 操作系统上编译的,所以 classpath 中的多个目录应该用冒号 (:) 而不是分号 (;) 分隔。所以这是新的复杂代码,我也包含了 jar 文件的完整路径,但它仍然找不到那些 jar 文件中定义的类。

javac -classpath ~/search_example/lucene-5.0.0/analysis/common/lucene-analyzers-common-5.0.0.jar:~/search_example/lucene-5.0.0/core/lucene-core-5.0.0.jar:~/search_example/lucene-5.0.0/queries/lucene-queries-5.0.0.jar:~/search_example/lucene-5.0.0/queryparser/lucene-queryparser-5.0.0.jar lucene_test/*.java 

最佳答案

提供类路径变量的完整路径或至少从当前目录中正确引用总是好的。

但是,使用像 maven 这样的构建工具总是好的,它有助于绕过依赖项和类路径。

关于java - 从命令行错误在 Linux 上编译 Java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29350868/

相关文章:

java - 如何在Java数据结构中实现多类别?

java - 为什么我使用javamail发送到outlook账户时回复邮件是以附件形式发送的?

java - 如何用Java编写GUI进行逻辑设计?

在c中创建一个pthread,使用它来等待创建线程完成执行

java - 使用Windows命令行在java中编译带有包的程序

java - IntelliJ 说 "\\"(匹配单个反斜杠)是 Pattern.compile 的非法/不受支持的转义序列

linux - Docker容器中挂载/dev/sda1到/etc/hosts是什么意思

linux - 这个 Makefile 是如何工作的?

optimization - 如何在 ocaml 中将 Flambda 与沙丘一起使用?

c++ - char* 和 unsigned char* 转换