从命令行错误编译Linux上的Java应用程序

时间:2015-03-30 16:00:37

标签: 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'文件'classpath'

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

1 个答案:

答案 0 :(得分:0)

总是很好地提供你的类路径变量的完整路径或至少从你当前目录的适当引用。

然而,使用像maven这样的构建工具总能很好地帮助绕过依赖项和类路径。