简体   繁体   English

奇怪的“找不到符号”错误(从终端编译包)

[英]Strange "cannot find symbol" error (Compiling package from Terminal)

First of all: I've read through all the cannot find symbol threads I could find here.首先:我已经阅读了所有我可以在这里cannot find symbol线程。 None of them solved the problem I'm facing.他们都没有解决我面临的问题。 I'm not a professional Java Developer and I'm just helping out a colleague with this classes so please go easy on me.我不是专业的 Java 开发人员,我只是在帮助一位同事上这门课,所以请放轻松。

Let me describe the basic situation first:先说一下基本情况:

I have a package called pdfDownload located at src/pdfDownload .我有一个名为pdfDownload的包,位于src/pdfDownload In this directory I've got 2 files: PDFItem.java and PDFItemParser.java .在这个目录中,我有 2 个文件: PDFItem.javaPDFItemParser.java

Both are public classes.两者都是公开课。 You can find the code of the classes attached below.您可以在下面找到这些类的代码。 I'm using Eclipse IDE shows no warnings nor errors**.我正在使用Eclipse IDE没有显示任何警告或错误**。

When I compile them I get the following error message:当我编译它们时,我收到以下错误消息:

 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

The classes are both public , in the correct directory and package .这些类都是公共的位于正确的目录和包中 I also get auto-completion in Eclipse for the PDFItem inside the PDFItemParser class.还在 Eclipse 中PDFItemParser类中的PDFItem自动完成 Me and my colleague have been struggling to solve this for 2 hours now.我和我的同事一直在努力解决这个问题 2 个小时。 I'm sorry if it's really easy for you guys but we couldn't solve it because the usual cases for this error didn't apply.如果这对你们来说真的很容易,我很抱歉,但我们无法解决它,因为此错误的通常情况不适用。 Thanks in advance!提前致谢!

Edit: I compile them in the (Mac) Terminal.编辑:我在(Mac)终端中编译它们。 I open the path in the Terminal, and type in:我在终端中打开路径,然后输入:

 javac PDFItem.java

and then进而

 javac PDFItemParser.java

PDFItem - Class Code: 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 */
        }

    }

You should compile your classes using this command to make sure that your classes go into a folder created for your package : -您应该使用此命令编译您的类,以确保您的类进入为您的package创建的文件夹:-

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

When you compile it without a -d flag, then your classes are not inside a package folder, where they are actually searched.当你在没有-d标志的情况下编译它时,你的类不在package文件夹中,它们实际上是在那里搜索的。 And hence your PDFItemParser is not able to find your PDFItem class.因此您的PDFItemParser无法找到您的PDFItem类。

Also, make sure that the you have added the path till your package folder in your classpath.另外,请确保您已将路径添加到类路径中的package folder中。 Add the path only till the folder with package name, and not till the class name.添加路径只到包名的文件夹,而不是类名。

Hey you don't have a proper main function.嘿,您没有适当的main功能。 Thats all:就这样:

I changed this line我改变了这条线

public static void main(){

to

public static void main(String args[]){

in PDFItemParser classPDFItemParser类中

Now i can run the program (But ofcourse it is giving runtime error )现在我可以运行程序(但当然它给出了运行时错误)

Error: data.csv (The system cannot find the file specified)null

EDIT编辑

This error is expected since i don't have data.csv file.由于我没有 data.csv 文件,因此预计会出现此错误。

I am able to compile and run this program without any issue, in eclipse我能够在 eclipse 中毫无问题地编译和运行这个程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM