0

I have a simple Java program that connects to a database. The connection is configured in db.properties file. My program works fine.

When I want to compile it into a executable jar file, I will place the properties file in the same folder so user can easily edit the properties file and change the connection settings to their liking. The slight problem here is my dbpath where I have to specify the exact directory to read the properties file.

I was wondering if I can save it in the same folder and set the path to current directory so no matter where the user save the folder, it will always be able to read the db properties without hardcoding the path.

Code:

 Properties props=new Properties();
                String dbpath = "C:\\Users\\nickywan123\\Documents\\db.properties";
                FileInputStream in = new FileInputStream(dbpath);

             props.load(in);
             in.close();

             String driver = props.getProperty("jdbc.driver");
             if(driver!=null){
              Class.forName(driver);

                }

                String url=props.getProperty("jdbc.url");
                String username=props.getProperty("jdbc.username");
                String password=props.getProperty("jdbc.password");

                Connection con = DriverManager.getConnection(url,username,password);

I've tried different solutions to get location of jar file such as Get location of JAR file but it doesn't return the location I want to be able to read it.

I appreciate any suggestion

Daredevil
  • 1,672
  • 3
  • 18
  • 47
  • Put the file into the `src/main/Resources` folder and have that put into your jar - have a look at the link I provided – Scary Wombat Nov 21 '18 at 04:38
  • You should use relative path for dependency files. e.g. `/resources/db.properties`. In this case resources folder should be parallel to the current working directory. – YetAnotherBot Nov 21 '18 at 04:39
  • possible duplicate of https://stackoverflow.com/questions/3457918/how-a-jar-file-can-read-an-external-properties-file – Shiva Nov 21 '18 at 04:42
  • @ScaryWombat So I have to include the folder along with that file in the folder that contains the jar file, is that correct? – Daredevil Nov 21 '18 at 04:54
  • If you want to be able to edit the file, then you need to store it on disk (externally). In this it's best to store it in a "well known" location. On windows that might be something like `System.getProperty("user.dir") + File.seperator + "AppData/{Appname}/{foldername}/{file}"`. The reason for doing something like this is because the working directory may not be the same location where the jar/files are installed – MadProgrammer Nov 21 '18 at 04:55
  • *"I will place the properties file in the same folder so user can easily edit the properties file"* - Makes this NOT a duplicate as the OP has a requirement for the file to be editable, which an embedded resource can not be – MadProgrammer Nov 21 '18 at 04:57
  • @MadProgrammer Agreed, not a dupe – Scary Wombat Nov 21 '18 at 04:58
  • I hope I was clear with what I am trying to achieve. Basically , the properties file just contain connection properties to a db. It should be editable by ANYONE and when they make the changes, they can run the jar file and it reflects on the changes. Where the user save or store the jar file does not matter because it's dynamic. That's my dilemma – Daredevil Nov 21 '18 at 05:02
  • @Daredevil If you prefer the user has the ability to specify the location of the file, then you need to provide a means by which they can select it, either via the command line or via a file browser – MadProgrammer Nov 21 '18 at 05:04
  • @MadProgrammer What do you mean by select? I assume they run in command prompt – Daredevil Nov 21 '18 at 05:06
  • @Daredevil The can pass in location/file name as a command line argument to your app – MadProgrammer Nov 21 '18 at 05:56

1 Answers1

2

I will place the properties file in the same folder so user can easily edit the properties file and change the connection settings to their liking

The problem you're facing comes down to the fact that the working directory, where the application is run, may not be the same as the location the application and its files are stored. You can not rely on the location been fixed.

Instead, you should adopt the conventions of the platform and store the files in a "common" location (which is fixed).

On windows that might be ...

System.getProperty("user.dir") + "/AppData/{App name}/{folder name}/{file}"

On MacOS that might be ...

System.getProperty("user.dir") + "/Library/Application Support/{App name}/{folder name}/{file}"

On Linux that might be ...

System.getProperty("user.dir") + ".{Appname}/{folder name}/file"

What about this, the jar file and properties file should be in the same FOLDER. Then user can decide where they want to store/keep the folder, which is the best solution to this?

Doesn't matter, you still run the risk of the "working directory" been different from the "installation directory", which is "why" the above solution was developed - this is not just a Java issue, but affects all programs

You may also run into issues with OS privileges ... looking at you Windows

"Another" solution is allowing the user to pass in the file/location via the command line, allowing them to run the Jar from anywhere, while allowing them to specify the location of the properties file they want to use ... meaning they could have more then one based on there needs

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for the reply, so if you store the file in a common location, wouldn't that be a problem because a user might want to store the jar file along with the properties file ,say in his own folder. And he just wants to amend the properties file and run the jar file so it reflects on the changes.? – Daredevil Nov 21 '18 at 05:05
  • That’s kind of the point - the jar and properties file are independent of each other, so the location in which the jar is installed, which can be different from where it’s run don’t effect your ability to find the file. The other solution is to allow the user to specify the file then selves, perhaps passing it as a command line argument – MadProgrammer Nov 21 '18 at 05:55
  • What about this, the jar file and properties file should be in the same FOLDER. Then user can decide where they want to store/keep the folder, which is the best solution to this? – Daredevil Nov 21 '18 at 06:03
  • Doesn't matter, you still run the risk of the "working directory" been different from the "installation directory", which is "why" the above solution was developed - this is not just a Java issue, but affects all programs – MadProgrammer Nov 21 '18 at 06:17
  • So keep the file properties in a different location than the jar file , is that right? – Daredevil Nov 21 '18 at 06:21
  • Essentially, keep it in a "well known" location, which you are guaranteed to have access to, such as the users home directory – MadProgrammer Nov 21 '18 at 06:23
  • So when I send the jar file and properties file to someone else, I need to inform that someone to keep the properties file in the fixed location, and nowhere else? As for the jar file, he can save it anywhere he wants? – Daredevil Nov 21 '18 at 06:26
  • @Daredevil Yes and no. You could do it that way and/or you could allow them to specify the location of the file via the commandline – MadProgrammer Nov 21 '18 at 06:46