FileNotFoundException when file exists with all permissions [duplicate]
Asked Answered
I

4

10

I am trying to read a file and the error i get is

java.io.FileNotFoundException: /homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations/gameTheoryAgentConfiguration.properties  (No such file or directory)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at game.player.gametheoryagent.GameTheoryAgent.<init>(GameTheoryAgent.java:67)
        at simulation.Simulator.createPlayer(Simulator.java:141)
        at simulation.Simulator.main(Simulator.java:64)

however the file does exist and just to double check i gave it 777 permissions, as shown below:

tui% cd /homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations
tui% ls -al
total 4
drwxrwxrwx 3 at1106 cs4 1024 2010-02-22 17:45 .
drwxrwxrwx 4 at1106 cs4 1024 2010-02-22 17:27 ..
-rwxrwxrwx 1 at1106 cs4  260 2010-02-22 17:31 gameTheoryAgentConfiguration.properties
drwxrwxrwx 6 at1106 cs4 1024 2010-02-22 17:41 .svn

Any ideas as to why I'm getting the FNF exception?

Thanks

java code that makes the call:

File file = new File(pathToConfiguration)
   Properties configuration = new Properties();
    try{
        configuration.load(new FileInputStream(file));
        int RAISE_RATIO = Integer.parseInt(configuration.getProperty("raise_ratio"));
    }
    catch(IOException event){
        System.err.println("Error in reading configuration file " + pathToConfiguration);
        event.printStackTrace();    
  }

The properties file reads:

raise_ratio=4

This was tested in windows (with a diff pathToConfiguration (which is passed into the constructor)) and works fine.

Added in the following checks in the Catch block

        if(file.exists()){
            System.out.println("file exists");
        }
        else{
            System.out.println("file doesn't exist");
        }

        System.out.println(file.getAbsolutePath());
        if(file.canRead()){
            System.out.println("can read");
        }
        if(file.canWrite()){
            System.out.println("can write");
        }

the output is as follows:

file doesn't exist
/homes/at1106/fourthYearComputing/Individual-Project/svn-workspace/trunk/Individual_Project/src/game/player/gametheoryagent/configurations/gameTheoryAgentConfiguration.properties
Inwards answered 22/2, 2010 at 17:55 Comment(12)
Can you paste the actual Java code?Staton
What code are you running when the exception is thrown? What happens if you use file.exists()? file.getAbsolutePath()? file.canRead/Write/Execute()?Clam
Are you running your java code on the same machine where the file exists?Filigree
Do you have SELinux or AppArmor enabled on that box?Ungotten
@Lord Torgamus I have edited the question to answer that. @Murali yes I am @disown I have no idea what those are is there any way to check?Inwards
What about pathToConfiguration variable? How did you initialize it? Can you also post the corresponding code to the variable?Dongdonga
@Dongdonga in the catch block I print out the pathToConfiguration which is the same as in the FNF exceptionInwards
How did you create variable file? It wasn't in your original post.Clam
@Torgamus do you mean the properties file? I created in a text editor. I then tried running dos2unix on it and no luckInwards
@Aly: No, he means the file variable, the one you use in if (file.exists()) etc...Gog
oh sorry, I done File file = new File(pathToConfiguration), I will update the questionInwards
In that case... what happens if you do file.createNewFile()?Clam
J
21

According to the initial stacktrace there appear to be two spaces between the file name and reason:

FileNotFoundException: ...Configuration.properties  (No such file or directory)
--------------------------------------------------^^

This would indicate to me that the filename possibly has a trailing space. Can you double check your pathToConfiguration variable by:

System.out.println("[" + pathToConfiguration + "]");

To double check that the path is what you think it is?

Ja answered 22/2, 2010 at 18:53 Comment(6)
Let me know if that was just a typo, if so I'll delete this answer.Ja
@beny: Even if this turns out to be wrong for this particular case, great idea.Clam
It's always a good idea to enclose your filename in quotes or brackets in error messages, just so this kind of thing is obvious. It's also a good idea to trim leading and trailing whitespace from filenames before using them.Baywood
gave another vote just because the same thing happened to me... same ol' stupid mistakeSupposition
FYI: Apparently this only fails on Linux, the same code on Windows does not throw this exception.Fewness
Wow I spent 4 hours trying to solve thisDorotea
C
0

When you execute your java program are you running it as the same 'user' as when you run the command-line checks?

EDIT: Try copying the file to the directory where you run your program from and see if it is able to read it. You can also try the following after copying the file to your execution directory:

InputStream in = getClass().getResourceAsStream("/gameTheoryAgentConfiguration.properties");
configuration.load(in);

(assuming you have "." in your classpath)

Careworn answered 22/2, 2010 at 18:18 Comment(1)
Aly, can you confirm that System.getProperty("user.name") yields at1106?Beriberi
P
0

I suppose you double checked the pathname more than once, and as you say you are running the app on the same machine where the code resides.

Could it be that there are some obscure NFS/file server mounts that are valid only for the login shell but not for the applications?

Try copying the file in your $HOME and see if it works.

Polyamide answered 22/2, 2010 at 18:25 Comment(1)
I managed to copy the file to $HOMEInwards
P
0

What gets outputted if you write this:

System.out.println(new File(".").getAbsolutePath());

what is your current directory?

Petronius answered 22/2, 2010 at 18:37 Comment(3)
this: /homes/at1106/fourthYearComputing/Induvidual-Project/svn-workspace/trunk/Induvidual_Project/.Inwards
Does your file exist in that folder?Petronius
no, but when i create the file i do so with the absolute pathInwards

© 2022 - 2024 — McMap. All rights reserved.