-2

I have a program in which the properties file is in the same path as the program I'd like to run.

The properties file name is initialized at the start of the program with:

public static final String DEFAULT_PROPERTIES_FILE = "defaultProperties.properties"; 

The program runs perfectly when running it in the directory it itself is located in with:

java -jar program.jar

When running the program from another directory with:

java -jar path/program.jar

The program will not find the properties file and returns a:

java.io.FileNotFoundException: defaultProperties.properties (No such file or directory)

FIXED

I came up with a solution. It is possible to call the path of the class and give the path to the configurations file.

StringBuilder stringBuilder = new StringBuilder( PolicyBoosterTool.class.getProtectionDomain().getCodeSource().getLocation().getPath() );
String pathToFolderOfJarFile = stringBuilder.substring( 0, stringBuilder.lastIndexOf( "/" ) + 1);

DEFAULT_PROPERTIES_FILE =  pathToFolderOfJarFile + "defaultProperties.properties";

Sadly I can't mark the case as solved for myself. Any improvements to this are always welcome. Thank you for your help everyone!

  • Put the parent folder of properties file in class-path – Sanjeev Jun 20 '16 at 09:25
  • 1
    How are you trying to read the file? Unless you've specified differently, it'll look for it in the current path where the jar is running from. – Ori Lentz Jun 20 '16 at 09:26
  • 1
    Please post both the code you use to read a file, and the full stacktrace of the exception you are receiving. – Nikem Jun 20 '16 at 09:29
  • 1
    Possible duplicate of [Confused about java properties file location](http://stackoverflow.com/questions/9150990/confused-about-java-properties-file-location) – Sanjeev Jun 20 '16 at 09:37
  • @Nikem I posted what I hope is all the crucial parts of the code and the full stack trace. Does this help? and thank you :) – Turbojumala Jun 20 '16 at 11:06
  • @OriLentz Then I should opt Boolas solution to "create a custom configuration class to get properties file from a fixed set of path" to achieve that I assume? Thank you! ^_^ – Turbojumala Jun 20 '16 at 11:06
  • @Sanjeev The properties file is in the program's path. I'd like the properties be freely alterable by the user and external from the program itself, so it's loaded as the program boots. Maybe I'm not understanding you correctly. Thanks though :) – Turbojumala Jun 20 '16 at 11:07

2 Answers2

0

You have to specify the complete path of the property files. If you don't specify the path, then it will try to find file in same directory from which you started the jar. Hence FileNotFoundException.

One simple solution is - define property file's path as command line argument.

Sampada
  • 2,931
  • 7
  • 27
  • 39
Boola
  • 358
  • 3
  • 14
  • Ah, I also want to be able to run it from different locations if the program is moved :) If possible I'd like to avoid also making others type the whole path they want to run the program. But I'll opt for that if there's no way to automate it. Thank you! – Turbojumala Jun 20 '16 at 09:29
  • if your properties files are at random path then you need to have a way to provide path to jar. You can create a custom configuration class to get properties file from a fixed set of path. Basically you have to opt for micro service architecture. – Boola Jun 20 '16 at 09:41
  • That sounds like exactly what I need! I'll try to look up how it's done, all though all suggestions are welcome ^_^ – Turbojumala Jun 20 '16 at 11:08
0

One way, to keep your properties file at any arbitrary location and make your application read it, is using VM params

for example:

Pass a VM Param say prop.path

java -Dprop.path=path/to/your/properties/file -jar path/program.jar

Then you can read it with

System.getProperty("prop.path")

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • Thank you, this would be a great solution if it were only for me. I'm looking for a way that other people could do the same with minimal effort. Ie. avoid typing all of java -Dprop.path=path/to/your/properties/file _before_ -jar path/program.jar. The answer was really helpful though, and I upvoted it. I hope I'll get enough karma some time for the upvote to show ^_^ – Turbojumala Jun 20 '16 at 13:37
  • @Turbojumala if you want to externalize your properties file then you have to provide a way to your program to get that path. either always put properties file in a folder that is in class path or specifically pass path to your program. – Sanjeev Jun 20 '16 at 14:27
  • Thank you! Yeah I figured out the solution. The properties file of course has to be in the path, but I wanted to be able to run the program from outside the path without needing to specify where the properties file is. I put my solution at the end of the question now. If you think there's a better way to solve the problem I'd be glad to hear it :) I think mine is OK at best :D Thanks for all your help! :) – Turbojumala Jun 21 '16 at 08:48