5

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!

Community
  • 1
  • 1
liloka
  • 1,016
  • 4
  • 14
  • 29

3 Answers3

11

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.

palacsint
  • 28,416
  • 10
  • 82
  • 109
  • Thank you! That does make it run but for some reason I'm getting "[Ljava.lang.String;@19821f" rather than "One" when it's being printed. Do you know why this is? :S Thank you :) – liloka Sep 10 '11 at 17:19
  • You are welcome. Change `System.out.println(aryLines)` to `System.out.println(aryLines[i])`. [More info about Array's toString()](http://stackoverflow.com/questions/7060016/why-does-tostring-method-in-java-doesnt-seem-to-work) – palacsint Sep 10 '11 at 17:26
  • Thank you.. I can't believe I missed that off in the first place! – liloka Sep 10 '11 at 17:36
7

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

?

David Moreno García
  • 4,423
  • 8
  • 49
  • 82
  • " I've made sure I have compiled ReadFile and fixed all the errors there. The compiler error I get is:" Yes I did :) It was due to location rather than a coding error. But thank you! – liloka Sep 10 '11 at 17:23
  • @liloka I meant at the same time. Not separately. Anyway I suppose that you did it. – David Moreno García Sep 10 '11 at 17:27
  • Ah, I ended up doing that anyway. Didn't even know you could compile at the same time. So thanks for that :) – liloka Sep 10 '11 at 17:37
  • You're welcome @liloka. That is the correct way to compile java programs. Good luck with your project ;) – David Moreno García Sep 11 '11 at 00:05
  • @DavidMorenoGarcía do you know why javac needs both files specified at the same time? For example I tried `javac file1.java` and `javac file2.java` and it gave the error, but `javac file1.java file2.java` works. – Celeritas Jul 03 '16 at 08:47
1

There must be a conflict with generated classes. Just try to remove all the classes that have been generated and build project again.

seradd
  • 53
  • 6