Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid FileNotFound exception when running Java on Linux because of case sensitiveness?

My web application runs on Windows. I would like to run my app on Linux also. I seem to have overcome most of the problems such as path separator, etc.

Real problem now is I get FileNotFoundException when the Java code tries to open a file say Abc.txt when only abc.txt exists. :(

I can't go on changing all the filenames to lowercase or uppercase as i have a whole lot of files. Without changing code much is that any possible way to avoid this?

like image 799
Senthil Kumar Avatar asked Nov 14 '09 09:11

Senthil Kumar


People also ask

How do I stop FileNotFoundException in Java?

How to avoid FileNotFoundException in java? Please ensure file exists when you try to read from file from path exists (using FileInputStream) to avoid FileNotFoundException. Please ensure file exists when we try to acces file from invalid path using RandomAccessFile to avoid FileNotFoundException.

Are Java files case sensitive?

Java is a case-sensitive language, which means that the upper or lower case of letters in your Java programs matter.

Is FileNotFoundException a runtime exception?

FileNotFoundException occurs at runtime so it is a checked exception, we can handle this exception by java code, and we have to take care of the code so that this exception doesn't occur.

How do I catch a file not found exception?

Handling FileNotFoundException In order to handle the exception, it is required to use the try-catch block. In the try block, we will put that line of code that can throw an exception. Whenever an exception occurs, the catch block will handle it.


2 Answers

Fix it!

Any scheme you devise to circumvent fixing it will be worse in the long run.

like image 188
Thorbjørn Ravn Andersen Avatar answered Oct 18 '22 08:10

Thorbjørn Ravn Andersen


There is no way to avoid this as the java.io.File API is system-dependent. You have to use the right case when manipulating files on Linux/Unix. Actually, my advice/solution would be to follow strict and portable conventions during development on Windows (e.g. use lower case filenames only or, better, use the exact file name when accessing it programmatically). To be honest, I don't understand why you are trying to load Abc.txt when the filename is abc.txt. This is a bad habit (taught by spending too much time on Windows) rather than a Linux/Unix problem.

like image 36
Pascal Thivent Avatar answered Oct 18 '22 07:10

Pascal Thivent