java - 奇怪的 "cannot find symbol"错误(从终端编译包)

标签 java compiler-construction compiler-errors

首先: 我已经通读了我可以在这里找到的所有找不到符号主题。他们都没有解决我面临的问题。我不是专业的 Java 开发人员,我只是帮助同事上这门课,所以请对我宽容一些。

先描述一下基本情况:

我有一个名为 pdfDownload 的软件包,位于 src/pdfDownload。在这个目录中,我有 2 个文件:PDFItem.javaPDFItemParser.java

两者都是公共(public)类(class)。您可以找到下面所附的类的代码。我使用 Eclipse IDE 没有显示任何警告或错误**。

当我编译它们时,我收到以下错误消息:

 PDFItemParser.java:19: cannot find symbol symbol  : class PDFItem
 location: class pdfDownload.PDFItemParser  public static
 ArrayList<PDFItem> parseFile(String filePath){
            ^ PDFItemParser.java:11: cannot find symbol symbol  : class PDFItem location: class pdfDownload.PDFItemParser
    ArrayList<PDFItem> items = parseFile("data.csv");    
              ^ PDFItemParser.java:20: cannot find symbol symbol  : class PDFItem location: class pdfDownload.PDFItemParser         ArrayList<PDFItem>
 items = new ArrayList<PDFItem>();      /* Creates an ArrayList from type
 PDFItem which will contain all parsed ItemObjects */
                  ^ PDFItemParser.java:20: cannot find symbol symbol  : class PDFItem location: class pdfDownload.PDFItemParser
        ArrayList<PDFItem> items = new ArrayList<PDFItem>();        /* Creates an
 ArrayList from type PDFItem which will contain all parsed ItemObjects
 */
                  ^ PDFItemParser.java:21: cannot find symbol symbol  : class PDFItem location: class
pdfDownload.PDFItemParser       items.add(new PDFItem());
                      ^ 5 errors

类都是公共(public)的位于正确的目录和包中。我还在 Eclipse 中获得了 PDFItemParser 类中 PDFItem 的自动完成功能。我和我的同事已经努力解决这个问题两个小时了。如果这对你们来说真的很容易,我很抱歉,但我们无法解决它,因为此错误的常见情况不适用。提前致谢!

编辑:我在(Mac)终端中编译它们。我在终端中打开路径,然后输入:

 javac PDFItem.java

然后

 javac PDFItemParser.java

PDFItem - 类代码:

    package pdfDownload;

    public class PDFItem {

        String imageURL;
        String pdfURL;
        boolean imageLoaded;
        boolean pdfLoaded;
        String name;


        public PDFItem() {

        }

        public PDFItem(String name) {
            this.name = name;
        }

    }


PDFItemParser - Class Code:
---------------------------

    package pdfDownload;

    import java.util.ArrayList;
    import java.io.*;


    public class PDFItemParser {

        public static void main(){

        ArrayList<PDFItem> items = parseFile("data.csv");    

        if(items != null){
            System.out.println(items.get(0).name);
        }
    }


        public static ArrayList<PDFItem> parseFile(String filePath){
            ArrayList<PDFItem> items = new ArrayList<PDFItem>();            /* Creates an ArrayList from type PDFItem which will contain all parsed ItemObjects */
            items.add(new PDFItem());

            try{
                FileInputStream fstream = new FileInputStream(filePath);
                DataInputStream in = new DataInputStream(fstream);              /* Get the object of DataInputStream */

                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                while ((strLine = br.readLine()) != null)   {           /* Read File Line By Line  */

                  System.out.println (strLine);             /* Print the content on the console */

                }
                in.close();             /* Close the input stream */

            }
            catch (Exception e){            /* Catch exception if any */
                System.err.println("Error: " + e.getMessage());
            }

            return items;               /* Returns ArrayList */
        }

    }

最佳答案

您应该使用此命令编译您的类,以确保您的类进入为您的创建的文件夹中:-

javac -d . PDFItem.java
javac -d . PDFItemParser.java

当您在没有 -d 标志的情况下编译它时,您的类不在实际搜索它们的 package 文件夹中。因此,您的 PDFItemParser 无法找到您的 PDFItem 类。

此外,请确保您已将路径添加到类路径中的包文件夹。仅添加路径到包含包名称的文件夹,而不添加到类名称。

关于java - 奇怪的 "cannot find symbol"错误(从终端编译包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13453735/

相关文章:

java - 使用另一个java程序编译一个java程序

ruby-on-rails - Rails应用突然无法正常工作

java - 使用 SSL 证书的 Java 连接 HTTPS 错误

java - 仅更改 ImageButton 背景的一种状态(默认状态)

java - 如何检查文本文件中的下一行是否为空?

java - 尝试使用 Spring 运行 jUnit 测试时出现 NoSuchFieldError

c++ - 我的交叉编译器总是编译同一个文件

java - 如何在 Java 中进行混淆器、优化器和混淆器

java - 错误: Cannot Find Symbol/No Spelling Mistakes?

r - 在OSX Catalina中cmath的Brew clang++问题