I have a .jar file I'm putting together. I want to create a really really simple .properties file with configurable things like the user's name & other stuff, so that they can hand-edit rather than my having to include a GUI editor.
What I'd like to do is to be able to search, in this order:
args[0]
)I know how to access #1 and #3 (I think), but how can I determine at runtime #2 and #4?
Simplest way, use the -D switch to define a system property on a java command line. That system property may contain a path to your properties file. Then, in your code you can do ( exception handling is not shown for brevity ): String propPath = System.
For 4, you could try this. Get the classpath:
String classpath = System.getProperty("java.class.path");
Then search it for the name of your application jar:
int jarPos = classpath.indexOf("application.jar");
Parse out the path leading up to it:
int jarPathPos = classpath.lastIndexOf(File.pathSeparatorChar, jarPos) + 1;
String path = classpath.substring(jarPathPos, jarPos);
Then append MyApp.properties
. Make sure to check for jarPos == -1
, meaning the jar isn't found if the classpath (perhaps when running in your dev environment).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With