0

I have 10 .png files that each have a number on them that im displaying in my program. each is entitled FB_1, FB_2 and so on respectively. when I try to load them into my Numbers.java file I'm having some trouble referencing them. I had them in a folder called res that was in the same directory as my src folder. I then loaded them as such.

for(int i = 0; i < 10; i++) {
        try {

            FB_[i] = ImageIO.read(new File("res/FB_" + i + ".png"));

        }catch(IOException e) {

            new JFrame("Error 403 - Can't read number Image Files!").
                setVisible(true);
        }
    }

This worked fine but when i sent the program to my buddy he ran it on another IDE (I think jGrasp) and he said he got an error loading the images.

I ran this code in jGrasp and it didn't work so I proceeded to change the line to this

FB_[i] = ImageIO.read(new File("FB_" + i + ".png"));

and it worked.. I also ran it through command prompt and it worked again..

Now, when I went back into Eclipse and tried running the new code, i began getting the exception saying that it couldn't locate the files. I know as a fact they are saved in the default package with the java files but I don't know why it only can't access them in Eclipse.

Does anyone know why this happens?? I'm amateur and am sort of new to working with files so it may be something dumb.

greg-449
  • 109,219
  • 232
  • 102
  • 145
Acor74u
  • 17
  • 5

1 Answers1

2

This is obviously a problem of specifying correct path to your files. Since you are not providing full path to the file your application tries to find them in the current working directory. This current working directory is where you run your application from.

If you set only the filename as in here new File("FB_" + i + ".png") then it will find files without going to any subdirectories. In your case it worked because you launched the app from the same place where you had your files.

To fix it either always run your app from the same place and have your files in the same place relative to your app, or provide a full path such as C:\files\FB_... for example.

Another thing you should consider is path delimiter / symbol that you use on Linux, or \ on Windows. In Java you can get it either from a constant or create a path by using File. See this for example: Does Java have a path joining method?.

Community
  • 1
  • 1
yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65
  • when I write "FB_" + i + ".png" I have the files in the working directory (aka the src folder and the default package in eclipse) but it still doesn't work in eclipse and I want this to run on any computer no matter where its saved eventually so i don't want to use the full path – Acor74u Feb 14 '14 at 03:51
  • It might be that you and your friend use different OSes like Windows and OS X and they have different path delimiters. – yǝsʞǝla Feb 14 '14 at 03:54
  • It's not really a problem of IDE, and even not Java. It's the same principle that applies to any file system path. It can be relative or absolute, and it might be delimited with `\` or with `/`. So when you access your files from any application or shell you need to have correct path to them depending on these factors. – yǝsʞǝla Feb 14 '14 at 03:57
  • I know hes using windows 7 and im on vista and i think that path delimiter thing is helping too thanks – Acor74u Feb 14 '14 at 04:02