I want java to look for .properties file in same folder with jar which is running. How can I do it and how can I determine if app is running in IDE or as explicit jar file
-
Not so great an approach. You should either 1) put the properties in the jar file or 2) put the properties somewhere relative to the current working directory (maybe changing the cwd before you launch the jar) or 3) put the properties at same well-known (hardcoded or configurable by environment) location (think /etc/myapp/config.props) – Thilo Feb 02 '15 at 10:28
-
But if you must: http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file?rq=1 – Thilo Feb 02 '15 at 10:29
-
There is a way, it's rather hackish, give me a couple of minutes and I'll find it among my files and post an answer. – Ceiling Gecko Feb 02 '15 at 10:30
3 Answers
you can access this file (if it is in your classpath) via:
Paths.get(this.getClass().getResource("file.properties").toURI());

- 341
- 1
- 12
Firstly I have to say that this is inherently a bad practice, though life often is not perfect and sometimes we have to roll with the bad design punches.
This is the class I mustered up for a pet project of mine that required such functionality:
public class ArbitraryPath {
private static Logger logger = LogManager.getLogger("utility");
private static boolean isRunFromJar = false;
public static String resolveResourceFilePath(String fileName, String folderName, Class<?> requestingClass) throws URISyntaxException{
// ARGUMENT NULL CHECK SAFETY HERE
String fullPath = requestingClass.getResource("").toURI().toString();
isRunFromJar = isRunFromJar(fullPath);
String result = "";
if(!isRunFromJar){
result = trimPathDownToProject(requestingClass.getResource("").toURI());
}
result = result+folderName+"/"+fileName+".properties";
return result;
}
private static String trimPathDownToProject(URI previousPath){
String result = null;
while(!isClassFolderReached(previousPath)){
previousPath = previousPath.resolve("..");
}
previousPath = previousPath.resolve("..");
result = previousPath.getPath();
return result;
}
private static boolean isClassFolderReached(URI currentPath){
String checkableString = currentPath.toString();
checkableString = checkableString.substring(0,checkableString.length()-1);
checkableString = checkableString.substring(checkableString.lastIndexOf("/")+1,checkableString.length());
if(checkableString.equalsIgnoreCase("bin")){
return true;
} else {
return false;
}
}
private static boolean isRunFromJar(String fullPath) throws URISyntaxException{
String solidClassFolder = "/bin/";
String solidJarContainer = ".jar!";
if(!fullPath.contains(solidClassFolder)){
if(fullPath.contains(solidJarContainer)){
return true;
} else {
logger.error("Requesting class is not located within a supported project structure!");
throw new IllegalArgumentException("Requesting class must be within a bin folder!");
}
} else {
return false;
}
}
}
I guess a little explaining is in order...
Overall this will try to resolve a filepath for a properties file located in an arbitrary project. This means that the ArbitraryPath class does not need to be located in the same project as the properties file, this comes in handy when you want to separate for example the JUnit tests in a separate project. It will identify the project based on the class you give it, the class should be in the same project as the properties file you are trying to find.
So first of all it gets the path of the class you gave it in this line:
String fullPath = requestingClass.getResource("").toURI().toString();
Then it checks whether or not this class is within a JAR file or if it is executed from the IDE. This is done by checking whether the path contains "/bin/" which would normally mean that it is executed from an IDE or ".jar!" which would normally mean that it is executed from a JAR. You can modify the method if you have a different project structure.
If it is determined that it is NOT run from JAR then we trim the path down to the project folder, going backwards down the path until we reach the BIN folder. Again, change this if your project structure deviates from the standard.
(If we determine that it IS run from a JAR file then we don't trim anything since we have already got the path to the base folder.)
After that, we have retrieved a path to the project folder so we add the name of the folder (if there is one) where the properties file is located, and add the filename and extension of the properties file which we want to locate.
We then return this path. We can then use this path in an InputStream like such:
FileInputStream in = new FileInputStream(ArbitraryPath.resolveResourceFilePath("myPropertiesFile", "configFolder", UserConfiguration.class));
Which will retrieve the myPropertiesFile.properties in the myConfiguration folder in the project folder where UserConfiguration.class is located.
Please note that this class assumes you have a standard project configuration. Feel free to adapt it to your needs etc.
Also this is a really hackish and crude way to do this.

- 3,104
- 2
- 24
- 33
String absolutePath = null;
try {
absolutePath = (new File(Utils.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath())).getCanonicalPath();
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator))+File.separator;
} catch (URISyntaxException ex) {
Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
}