saeed hardan
saeed hardan

Reputation: 485

Running jar file / .class file along with a properties file

I have an eclipse project that uses a Properties file. The file is located in the root directory of the project. I want to be able to run the .class file that eclipse builds using the cmd (in windows).

I use this code to get the properties from the file:

Properties prop = new Properties();
FileInputStream fileStream = new FileInputStream("DBConfig.properties");
prop.load(fileStream);

but when I run the .class file I get this error:

couldn't read property file...

How can I do this?

I also did see a post similar to this, but he needed to override a system property, which I don’t: Execute a jar with an external properties file.

Upvotes: 2

Views: 4563

Answers (2)

Dave Newton
Dave Newton

Reputation: 160291

Put the property file on your classpath and read it as a resource.

If you need to provide external configuration (e.g., changeable by editing the text file) then use a command line parameter or system parameter (e.g., -Dconfig=/a/full/path) to locate the file.

Upvotes: 2

Vinay Lodha
Vinay Lodha

Reputation: 2223

try passing properties file as parameter something like below

java -jar <jar name> -DDBConfig.properties=<Path>

And code will be something like

FileInputStream fileStream = new FileInputStream(System.getProperty("DBConfig.properties"));

Upvotes: 1

Related Questions