Search code examples
javaobjectcompiler-errorscommand-prompt

I receive "can't find symbol" errors while calling a constructor from another separate class in command prompt


I am trying to call the class FileRead and methods Display and Contents from another separate class, however it is within the same package. This is my main class. It works within Eclipse. I can type in a text file and it displays it. This class also compiles and runs in command prompt.

public class FileRead 
{
    static String theFile;
    Scanner myScanner = new Scanner (System.in);
    static List<String> fileArray = new ArrayList<String>();
    static StringBuffer stringBuff = new StringBuffer();
    
    public FileRead()
    {   
        System.out.println ("Enter your file: ");
        theFile = myScanner.nextLine();
        
        try 
        {
            myScanner = new Scanner(new File(theFile));
            FileReader myFileReader = new FileReader(theFile);
            BufferedReader buffRead = new BufferedReader(myFileReader);
            while ((theFile = buffRead.readLine()) != null) 
            {
                fileArray.add(theFile);
            }           
            
            buffRead.close();
        } 
        
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
        
    public String Contents()
    {
        for (String str : fileArray) 
        {
            stringBuff.append(str);
            stringBuff.append("\n");
        }
        
        String str = stringBuff.toString();
        return (str);
    }
    
    public void Display()
    {   
        for (int i = 0; i < fileArray.size(); i++)
        {
            System.out.println ("Line # " + (i + 1) + " " + fileArray.get(i));
        }
    }
    
    public static void main (String[] args)
    {
        //FileRead textFile = new FileRead();
        //textFile.Display();
        //textFile.Contents();
    }
}

The problem I am having is with this class:

public class FileReadShow
{   
    public static void main (String[] args)
    {   
        try
        {
            FileRead myTextFile = new FileRead();
            myTextFile.Contents();
            myTextFile.Display();
        }
        
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

When I try to compile this in the command prompt, I get this:

javac filereadshow.java
filereadshow.java:112: error: cannot find symbol
                        FileRead myTextFile = new FileRead();
                        ^
  symbol:   class FileRead
  location: class FileReadShow
filereadshow.java:112: error: cannot find symbol
                        FileRead myTextFile = new FileRead();
                                                  ^
  symbol:   class FileRead
  location: class FileReadShow
2 errors

I've done some searching around and found the main causes of these errors. I didn't find any typos. I also tried removing one main methods from one of these classes but that hadn't worked either. One thing I'm still unsure of is undeclared variables, which is my first question.

1: I am new to constructors, and it looks correct to me, but could the problem be that I have incorrectly called the reference to the object where the FileRead constructor is?

2: I have narrowed the errors down to this statement alone FileRead myTextFile = new FileRead(); because no matter what I do with it, this is where the errors are. Could someone please help me determine if the problem is with the FileRead script itself, or with the object reference FileRead myTextFile = new FileRead();?

Thanks for your time!

EDIT: FileRead.class is in the current directory as I am able to compile it. I compiled that before trying FileReadShow.

EDIT #2: I switched IDE's and found more information which gave me two compile error descriptions:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:         -FileRead cannot be resolved to a type         
-FileRead cannot be resolved to a type

So far there doesn't seem to be a very definitive answer. I am currently working through fixes that have worked for other people. Such as restarting eclipse/PC, using the "clean" option in the file drop down menu, creating a new class, etc. So far nothing has worked. I will keep trying. Does anyone have any more suggestions I could try?

EDIT #3: Both of my .java files are inside of the package folder that contains everything for Eclipse (.CLASSPATH, .project, src folder, bin folder, etc). Normally when I change directories in command prompt, I go to this package folder and can compile with ease. My exact command prompt input is:

C:\Users\D>cd eclipse-workspace\packageone\src\packageone

C:\Users\D\eclipse-workspace\PackageOne\src\PackageOne>javac fileread.java
//As you can see the first one compiled, so I call the second below
C:\Users\D\eclipse-workspace\PackageOne\src\PackageOne>javac filereadshow.java
filereadshow.java:90: error: cannot find symbol
                        FileRead myTextFile = new FileRead();
                        ^
  symbol:   class FileRead
  location: class FileReadShow
filereadshow.java:90: error: cannot find symbol
                        FileRead myTextFile = new FileRead();
                                                  ^
  symbol:   class FileRead
  location: class FileReadShow
2 errors

The first .java file (FileRead.java) compiled, the second one (.FileReadShow.java) still results in errors. What am I doing wrong? I've compiled dozens of programs this way and haven't run into this...?


Solution

  • You must have your .java files in the correct directory structure, taking your packages into account.

    So if you have:

    package x;
    
    class Foo {}
    

    and

    package x;
    
    class Bar {
            Foo foo;
    }
    

    They should be at the path x/Foo.java and x/Bar.java relative to the directory where you run javac.

    So if you run javac x/Foo.java and javac x/Bar.java

    You'll get:

    .
    └── x
        ├── Bar.class
        ├── Bar.java
        ├── Foo.class
        └── Foo.java
    

    When javac Bar.java looks for Foo.class, it looks for x/Foo.class, because Foo is in the package x. So the directory structure that your files are in must mirror the package structure of your classes.