I am developing a Java Application (not a web app, no server etc) using Netbeans 7.0.1 IDE.
Right now I have an issue with reading any properties /or any other file from my java class.
Here is what I am doing
FileReader f = new FileReader( new File(args[0]) );
BufferedReader r = new BufferedReader(f);
in the argument I pass the file name like connector.properties (I have the file in the same package as this class). the project when run gives the error below
Can't load: connector.properties : Exception is: connector.properties (The system cannot find the path specified)
why am I not able to read the properties file? whats the root of my classpath?
when I set the 'Working Folder' (right clicking the project and selecting ‘Run’ section) to the package level like C/complete/path/to/the/package
only then the properties file is read.
I want to know why do I need to set the working folder in Netbeans project and how can I read the file without setting the working folder?
This is because you are not using a class path reader to read your properties file. Your above code reads a hard path.
And, to read a properties file from a class path, you can use apache commons Configuration’s file properties reader. There are several options there for reading properties file and the stream these input files are coming from e.g. Classpath.