How do relative file paths work in Eclipse?


So my 2009 new years resolution is to learn Java. I recently acquired "Java for Dummies" and have been following along with the demo code in the book by re-writing it using Eclipse. Anyway, every example in the book that uses a relative path does not seem to read the .txt file it's supposed to read from.

Here is the sample code:

import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridLayout;

class TeamFrame extends JFrame {

    public TeamFrame() throws IOException {
        PlayerPlus player;
        Scanner myScanner = new Scanner(new File("Hankees.txt"));

        for (int num = 1; num <= 9; num++) {
            player = new PlayerPlus(myScanner.nextLine(), myScanner.nextDouble());

        add(new JLabel());
        add(new JLabel("   ------"));
        add(new JLabel("Team Batting Aberage:"));
        add(new JLabel(PlayerPlus.findTeamAverageString()));
        setTitle("The Hankees");
        setLayout(new GridLayout(11,2));

    void addPlayerInfo(PlayerPlus player) {
        add(new JLabel(player.getName()));
        add(new JLabel(player.getAverageString()));

And you can see in the below screen shot I have included this file.

image no longer available

Also, I have verified that when I build the application that a copy of Hankees.txt is placed in the bin folder with the compiled .class files.

Lastly, if I change line 12 to the following and place Hankees.txt in the root of my C:\ drive the program compiles and runs fine.

Scanner myScanner = new Scanner(new File("C:\\Hankees.txt"));

So basically, my question is what am I doing wrong? Or is Eclipse responsible for this in some way?

Thanks for any and all help!

4/6/2017 7:12:57 PM

Accepted Answer

You need "src/Hankees.txt"

Your file is in the source folder which is not counted as the working directory.\

Or you can move the file up to the root directory of your project and just use "Hankees.txt"

1/12/2009 11:10:25 PM

This is really similar to another question. How should I load files into my Java application?

How should I load my files into my Java Application?

You do not want to load your files in by:


this is bad!

You should use getResourceAsStream.

InputStream inputStream = YourClass.class.getResourceAsStream(“file.txt”);

And also you should use File.separator; which is the system-dependent name-separator character, represented as a string for convenience.


A project's build path defines which resources from your source folders are copied to your output folders. Usually this is set to Include all files.

New run configurations default to using the project directory for the working directory, though this can also be changed.

This code shows the difference between the working directory, and the location of where the class was loaded from:

public class TellMeMyWorkingDirectory {
    public static void main(String[] args) {

The output is likely to be something like:


Yeah, eclipse sees the top directory as the working/root directory, for the purposes of paths.

...just thought I'd add some extra info. I'm new here! I'd like to help.


You can always get your runtime path by using:

 String path = new File(".").getCanonicalPath();

This provides valuable information about where to put files and resources.


Paraphrasing from

The classes under resolve relative pathnames against the current user directory, which is typically the directory in which the virtual machine was started.

Eclipse sets the working directory to the top-level project folder.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]