Include A Properties File With A Jar File
Asked Answered
M

5

8

I've written a small application. I've put the database specific information in a properties file.

db.url=jdbc:mysql://localhost:3306/librarydb
db.user=root
db.passwd=pas$w0rd

When I build the application to get the executable jar file, the properties file gets included inside. The whole purpose of putting those information in a properties file is to allow the user to change those but this is making it impossible.

How can I include a properties file with the jar file not in it? Kinda like you do with AppSettings file in .NET applications.

I'm very new to Java so I'd appreciate if you could break it down to a set of steps.

Thank you.

EDIT :

This is how I'm getting the values from the properties file in my program.

public class DatabaseHandler {

    public static Connection connectToDb() {
        Connection con = null;
        Properties props = new Properties();
        InputStream in = null;

        try {
            in = DatabaseHandler.class.getResourceAsStream("database.properties");
            props.load(in);
            in.close();

            String url = props.getProperty("db.url");
            String user = props.getProperty("db.user");
            String password = props.getProperty("db.passwd");

            con = DriverManager.getConnection(url, user, password);
        } catch (Exception ex) {        
            Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex); 
        }
            return con;
    }

}
Monde answered 19/5, 2013 at 7:24 Comment(6)
It's not clear to me what you mean by "How can I include a properties file with the jar file not in it" - just don't put the properties file in the jar file...Canzonet
Um...the closest example I can give is the App.config file in .NET. When the executable is created, the file is bundled with it, right? Not inside it.Monde
Sure. So now you've provided the code, we can see what you need to do - just use a FileInputStream instead of getResourceAsStream.Canzonet
Hi Isuru, I was looking for the same info.. Could you pls share some info on where I should be placing the properties file? And should I use FileInputStream or getResourceAsStream?Gonococcus
@Shruti Hi. Unfortunately I asked this question 2 years ago while working on a university project. I can't remember any of it now. And I'm not a Java developer anymore. It would be better if you could ask it from someone who's posted an answer below. Or post a separate question entirely. Sorry.Monde
@Isuru.. no problem, Let me check that againGonococcus
H
6

You are right. If the properties are intended to be modifiable by the user, then putting them in a properties file in the JAR is not going to work

There are a couple of approaches:

  • You could put the properties in a property file that you store in the file system. The problem is figuring out where in the filesystem to store them:

    • On UNIX / Linux there are conventions for where to store system-wide and per-user configuration properties, but these are a bit loose, and in a bit of a state of flux (e.g. certain "desktop" frameworks have their own preference mechanisms.

    • There are similar conventions for Windows I believe.

    • The problem with approach can be permissions issues (you don't want one user changing another's preferences), and possibly even issues with read-only file systems and the like.

    • If you are going to use a property file in the filesystem, it is a bad idea to hardwire the location into your code. Make it configurable via a command line argument and/or a "system property" that can be set using the -D option.

  • On Windows, you could put the properties into the Windows registry. But you will need to use 3rd party libraries to do this. And of course, this is not portable - it won't work on a non-Windows platform.

  • You could use the Java Preferences API. This is portable across multiple platforms but it is a Java-centric solution; i.e. it doesn't fit well with the platform's "normal way" of doing things.

In short, there is no solution that is ideal from all perspectives.


But once you've decided how to store your preferences, you could write your application so that it uses the properties file in your JAR file to provide default or initial settings.

Herbst answered 19/5, 2013 at 8:5 Comment(0)
G
2

You can load a property file from the classpath instead. This enables you to bundle the property file inside the jar, but also outside the jar. It just needs to be on the classpath of the running application.

You can do so in the following way:

Properties props = new Properties();
InputStream inputStream = this.getClass().getClassLoader()
        .getResourceAsStream(propFileName);

props.load(inputStream);
Gromwell answered 19/5, 2013 at 8:1 Comment(1)
So if I just put the properties file in the same folder where the executable jar file is, would it work? (I've also edited the question to put the code I'm using to retrieve the values from the properties file)Monde
D
2

instead of

in = DatabaseHandler.class.getResourceAsStream("database.properties");

try

in = DatabaseHandler.class.getResourceAsStream("/database.properties");
Debora answered 22/1, 2014 at 15:22 Comment(0)
S
0

Do not hardcode the location of property file. Your approach should be to stream the file from classpath.

If not, the better way would be to point to a URL which returns the properties information in a specific data format (say JSON).

If you have JAR check this out.

If your application is WAR check properties in WAR.

Shady answered 19/5, 2013 at 8:0 Comment(0)
P
0

If you are using springboot-based java microservices, you can try spring cloud config. It is very powerful when it comes to managing configs. It is centralised, allows you to manage the configs realtime and lets the app use the updated configs without the need to restart applications (@RefreshScope is used for that). Not only this will keep configs consistent across all the apps, but also you can secure it further by storing the configs in a centralised vault. These also support environment specific configs.

Proctor answered 13/5, 2022 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.