Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top