Cannot Find Symbol for another class file

41,612

Solution 1

Make sure the java files are all in the textfiles directory:

textfiles/FileData.java
textfiles/ReadFile.java

And run:

javac textfiles/FileData.java textfiles/ReadFile.java 
java textfiles.FileData

Your code works without any modification. I think you are compiling from a wrong directory:

C:\Users\Liloka\Source>javac FileData.java

Move the FileData.java to the textfiles directory.

Solution 2

You have to compile all the java files used by your main class. As ReadFile is used by FileData you have to compile it too.

Did you tried

javac Filedata.java ReadFile.java

or

javac *.java

?

Share:
41,612
liloka
Author by

liloka

I'm a graduate with a first in Computer Science, now a grad dev at ThoughtWorks. I love programming, Human Computer Interaction, dancing, travelling and photography. I'm the treasurer for the Manchester Metropolitan Computing Society, and do a number of talks involving testing, Git, and LaTeX. I'm also a StemNET ambassador, and will be running my first {Code Club} next term.

Updated on July 09, 2022

Comments

  • liloka
    liloka almost 2 years

    I've had this problem a few times, where I've created another class file and the main class file can't find it. Here's the main class file:

    package textfiles;
    
    import java.io.IOException;
     public class FileData
     {
    
    public static void main(String[] args)
    {
        String file_name = "Lines.txt";
    
        try {
            ReadFile file = new ReadFile(file_name);
            String[] aryLines = file.OpenFile();
    
            for(int i =0; i<aryLines.length; i++)
            {
                System.out.println(aryLines);
            }
        }
    
        catch(IOException e)
        {   
            System.out.println(e.getMessage());
        }
    }
      }
    

    Here is the class file it can't find:

    package textfiles;
    
    import java.io.IOException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    
     public class ReadFile
     {
    private String path;
    int numberOfLines=0;
    
    public ReadFile(String file_path)
    {
        path = file_path;
    }
    
    public String[] OpenFile() throws IOException
    {
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
    
        int numberOfLines = readLines();
        String[] textData = new String[numberOfLines];
    
        for(int i=0; i<numberOfLines; i++)
        {
            textData[i] = br.readLine();
        }
    
        br.close();
        return textData;
    }
    
    int readLines() throws IOException
    {
        FileReader file_to_read = new FileReader(path);
        BufferedReader bf = new BufferedReader(file_to_read);
    
        String aLine;
    
        while((aLine = bf.readLine()) != null)
        {
            numberOfLines++;
        }
    
        bf.close();
        return numberOfLines;
    }
      }
    

    I've tried running javac textfiles\ReadFile.java and javac textfiles\FileData.java as a suggestion for this. That doesn't work. I've made sure I have compiled ReadFile and fixed all the errors there. The compiler error I get is:

    C:\Users\Liloka\Source>javac FileData.java
    FileData.java:13: cannot find symbol
    symbol  : class ReadFile
    location: class textfiles.FileData
                        ReadFile file = new ReadFile(file_name);
                        ^
      FileData.java:13: cannot find symbol
      symbol  : class ReadFile
      location: class textfiles.FileData
                        ReadFile file = new ReadFile(file_name);
                                            ^
      2 errors
    

    I'm using notepad++and .cmd so it can't be an IDE error. Thanks in advance!