javacompiler-errors

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

Let me describe the basic situation first:

I have a package called pdfDownload located at src/pdfDownload. In this directory I've got 2 files: PDFItem.java and PDFItemParser.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**.

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. Me and my colleague have been struggling to solve this for 2 hours now. 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. I open the path in the Terminal, and type in:

 javac PDFItem.java

and then

 javac PDFItemParser.java

PDFItem - Class Code:

    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 */
        }

    }

Solution

  • You should compile your classes using this command to make sure that your classes go into a folder created for your 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. And hence your PDFItemParser is not able to find your PDFItem class.

    Also, make sure that the you have added the path till your package folder in your classpath. Add the path only till the folder with package name, and not till the class name.