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;
}
}
The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load . properties file in the form of key-value pairs.
Application properties packaged inside your jar ( application. properties including YAML and profile variants). Default properties (specified using SpringApplication.
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.
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);
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