0

It's an example on Thinking In Java,but i can't run it.

package test;

import java.io.*;

public class Testa {
    public static String read(String filename) throws IOException{
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String s;
        StringBuilder sb = new StringBuilder();
        while((s = in.readLine()) != null){
            sb.append(s + "\n");
        }
        in.close();
        return sb.toString();
    }

    public static void main(String [] args) throws IOException{
        System.out.println(read("Testa.java"));
    }
}

When I debug, Eclipse shows that "Source not found" enter image description here

I'm a freshman and have no idea of it. I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. Could you please help me?

3
  • Could you post the full error logs here? Commented May 13, 2017 at 13:03
  • 1
    Download the jre with source at Oracle if you really want to look inside Commented May 13, 2017 at 13:09
  • I am really appreciate it for your answer.Thanks! Commented May 13, 2017 at 13:43

1 Answer 1

1

This isn't an error, Eclipse is just telling you it doesn't have the source code for the FileReader libary class. You don't need the source code to run your program. If you install a JDK rather than a JRE Eclipse will be able to find the source of this library class since the JDK contains the source code for the library classes.

It looks like what you are really getting is a FileNotFoundException which means that your file doesn't exist, at least not in the place you have told the program to look.

The file path you are using to read the file is just "Testa.java". This means that Java will look in the 'current directory' to find Testa.java. When you run your program the current directory is not the directory containing your program's source code so this doesn't work and you get the exception.

One way to fix this is to specify the full path of the file to read, on Windows this would be something like "C:\\path\\to\\workspace\\project\\src\\Testa.java"

Sign up to request clarification or add additional context in comments.

1 Comment

I couldn’t have done it without you. Thank you for your answer sincerely!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.