3

I have created a Java program to compare scripts saved as files in the version management tool to those loaded in our database. It's a simple program, runs through start to finish and outputs to the console when it finds a discrepancy. Now I want to load the database URL, username and password as well as the location of my files from a .properties file.

I did assume that if I put the file on the classpath it would be visible from my Java program:

Properties values = new Properties();
try
{
    File checkPackages = new File("myfile.properties");
    if(!checkPackages.exists()) throw new FileNotFoundException();

    values.load(new FileReader(checkPackages));
}
catch(FileNotFoundException fnfe) {}

I also wanted to save this whole program to a .jar file so that it would be that bit more usable. Unfortunately, the only way I have found to reference the .properties file is to have it in the directory where I am running java.exe. The PATH or the CLASSPATH don't seem to apply??

I found an Oracle site about the .jar file's Manifest file as I was hoping there'd be an answer there, but the Class-path: element in the manifest only seems to refer to .jar files that are not in the .jar (and not .properties files that are!) Questions: Is there any way to wrap the .properties file into the .jar file so that my user doesn't have to know it is there? Is there any way to wrap the Oracle driver .jar into the app's .jar so my user doesn't have to know it is there (Oracle says this needs 'custom code')?

TIA

3 Answers 3

2

You can get the resources in your classpath (even when sealed in the JAR) by using the ClassLoader#getResource() and ClassLoader#getResourceAsStream() methods.

For example:

Properties values = new Properties();
values.load(ThisClass.class.getClassLoader().getResourceAsStream("myproject.properties"));
// umm, don't forget to close the stream, this code is just an example usage

Note that storing the username and password to any database in a program is considered a heavy security risk.

1
  • This was exactly what I was looking for when it comes to the .properties file. Thanks. There are a few other posts about loading the .jar dynamically. They all seem to agree that it's difficult to get right though, and may be best left done the usual way. Commented Jun 1, 2013 at 23:05
1

One of the appraoch can be to 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.

E.g

java -cp ... -Dmyproject.properties=/path/to/my.app.properties my.package.App

Fetch the property in your code as mentioned here:

String propPath = System.getProperty( "myproject.properties" );

final Properties myProps;

final FileInputStream in = new FileInputStream( propPath );

 try
 {
     myProps = Properties.load( in );
 }
 finally
 {
     in.close( );
 }
0

Well, I would recommend to encrypt the sensitive data (username, password, url in this case) with public and private keys rather than hiding it. It is afterall not hard to deflate any jar file (which is essentially a zip format) and trace the .properties file

Not the answer you're looking for? Browse other questions tagged or ask your own question.