Usually when you do a new java project, there are several essential file such log4j properties file, other properties file and log file and you need to tell java where they are and where do you want to save the log file.
I don’t want to hardcode the absolute path to these file because I am putting the projects in different machines at different development stages. For example, I using Eclipse/PC in dev stage, Ant/Unix in UAT and Production machine.
Here is my usual setup:
Project:
–src
–build
–conf
–log
–output
In my understanding, src and build directory are included in the classpath and could be read right away by this.getClass().getClassLoader().getResource(".").getPath()
. However, there is not a logical method to locate the Project directory programmatically.
If I could locate the Project directory programmatically, I could tell the program that the files are at ${Project}/conf and save the log file in ${Project}/log.
Should I just use a shell script to define these variable before running the program?
I have done something similar in the project I am currently building. I am having to access properties files that I have stored within the build, as well as outside the project. Which means that the files will be located outside of the resulting jar. So hopefully my method is helpful to you.
To access the internal file which is located inside the jar I use
Classname.class.getClassLoader().getResourceAsStream(fileName)
And the external files I use
new FileInputStream(fileName);
fileName is declared with the extension as well as any subfolders before it
eg. “thisfolder/thisfile.me”