Include properties file with jar file

I wrote a small application. I put information about a specific database in the properties file.

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

When I create an application to get the jar executable, the properties file is included internally. The whole purpose of putting this information in the properties file is to allow the user to modify them, but this makes it impossible.

How to include a properties file with a jar file in it? As if you are doing with the AppSettings file in .NET applications.

I am very new to Java, so I would appreciate it if you could break it down into several stages.

Thanks.

EDIT:

This is how I get 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; } } 
+7
source share
4 answers

You're right. If the properties are intended for modification by the user, then placing them in the properties file in the JAR will not work

There are several approaches:

  • You can put properties in a properties file that you store on the file system. The problem is where to store them in the file system:

    • On UNIX / Linux, there are conventions about where system-wide and user-defined configuration properties are stored, but they are a bit loose and a bit in a thread state (for example, certain โ€œdesktopโ€ frameworks have their own preference mechanisms.

    • There are similar conventions for Windows that I consider.

    • The problem with the approach may be a permission problem (you donโ€™t want one user to change other settings) and maybe even problems with read-only file systems, etc.

    • If you intend to use a properties file in the file system, it is a bad idea to make location difficult in your code. Make it custom with a command line argument and / or a "system property", which can be set using the -D option.

  • On Windows, you can put properties in the Windows registry. But for this you need to use third-party libraries. And, of course, this is not portable - it will not work on a platform other than Windows.

  • You can use the Java Preferences API . It is portable across multiple platforms, but it is a Java oriented solution; that is, it is not well suited for the platform in the "normal way" for doing things.

In short, there is no perfect solution from all points of view.


But as soon as you decide to save your settings, you can write your application so that it uses the properties file in your JAR file to provide standard or initial settings.

+6
source

Instead, you can load the properties file from the class. This allows you to combine the properties file inside the jar, as well as outside the jar. It just needs to be on the way to the class of the running application.

You can do it as follows:

 Properties props = new Properties(); InputStream inputStream = this.getClass().getClassLoader() .getResourceAsStream(propFileName); props.load(inputStream); 
+2
source

instead

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

try

 in = DatabaseHandler.class.getResourceAsStream("/database.properties"); 
+2
source

Do not copy the location of the properties file. Your approach should be to stream the file from the classpath.

If not, the best way would be to specify a URL that returns property information in a specific data format (e.g. JSON).

If you have a JAR, check this one out .

If your WAR application is checking properties in a WAR .

0
source

All Articles