Cannot Find Symbol for another class file

2019-01-28 17:45发布

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!

2条回答
看我几分像从前
2楼-- · 2019-01-28 18:18

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

?

查看更多
叼着烟拽天下
3楼-- · 2019-01-28 18:22

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.

查看更多
登录 后发表回答