Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why FileNotFoundException is thrown while it exists on linux

Tags:

java

file

linux

This is the first time i have encounter such problem with file access by Java on linux. The problem is just like the header says - FileNotFoundException is thrown when file actually exists. Moreover application with same configuration (props.txt file) runs like it should on windows. Let me provide a little bit of console output

datasu@dedi2392:~/netcrawler/dkpto$ ls -l
total 20
-rwxrw-rw- 1 datasu datasu  114 Aug  7 15:53 autoupdate
drwxr-xr-x 4 datasu datasu 4096 Aug  8 11:57 data
drwxr-xr-x 2 datasu datasu 4096 Aug  8 11:57 log
-rw-rw-rw- 1 datasu datasu   32 Aug  8 12:44 props.txt
-rwxrw-rw- 1 datasu datasu  126 Aug  8 12:55 propsUpdate
datasu@dedi2392:~/netcrawler/dkpto$ ./propsUpdate
Parent: /usr/home/datasu/netcrawler/dkpto
   1# -> propsUpdate
   2# -> autoupdate
   3# -> props.txt
   4# -> data
   5# -> log
 (No such file or directory)ava.io.FileNotFoundException: /usr/home/datasu/netcrawler/dkpto/props.txt
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.(Unknown Source)
        at netcrawler.Autoupdater.readProperties(Autoupdater.java:71)
        at netcrawler.Autoupdater.start(Autoupdater.java:54)
        at netcrawler.Autoupdater.main(Autoupdater.java:47)
datasu@dedi2392:~/netcrawler/dkpto$ java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
datasu@dedi2392:~/netcrawler/dkpto$

and here is Java code responsible for generating that output (at least after calling ./propsUpdate)

private void readProperties(String args) throws FileNotFoundException, IOException {
    System.out.println("Parent: " + new     File(args).getAbsoluteFile().getParentFile().getAbsolutePath());
    CommonTools.PrintArray(new File(args).getAbsoluteFile().getParentFile().list());
    properties.load(new FileInputStream(new File(args).getAbsoluteFile())); // this line throws the exception
    stageNumber = Integer.parseInt(properties.getProperty(PROP_STAGE_NUMBER_KEY, "0"));
}

So why the props.txt file is not found when it is actually there ?

like image 979
Antoniossss Avatar asked Aug 08 '13 11:08

Antoniossss


2 Answers

The string "args" probably has a nonprinting character at the end, like a space. You could use String.trim() to remove such characters before using that variable.

like image 113
Ernest Friedman-Hill Avatar answered Nov 17 '22 02:11

Ernest Friedman-Hill


Is your home folder really this path?

/usr/home/datasu

/home/datasu is where it normally is on linux.

Also, try changing that line to this:

properties.load(new FileInputStream(new File(args));

If you're calling that as ./propsUpdate ./props.txt that will work from the current working directory.

like image 21
Mike Thomsen Avatar answered Nov 17 '22 02:11

Mike Thomsen