Itsik Mauyhas
Itsik Mauyhas

Reputation: 3994

Setting system property to a file path

I am trying to set System.setProperty to a file path:

//properties key
String propFile = "propertiesFile";
String pathToFile = "properties/prop.properties";
File file = new file(pathToFile);
//properties value
String path = file.getAbsolutePath();
System.setProperty(propFile, path);
//using properties.....

And I am getting FileNotFoundException. when printing the file I get the absolutePath - c:\Project...\prop.properties Is setting System.setProperty should be done another way? the properties - package inside src.

Upvotes: 1

Views: 5221

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 44995

Your problem is not related to the method System.setProperty as your path is managed as any other String, your problem is more that new File(pathToFile) refers to a non existing file as you provide a relative path and the absolute path is created from the user directory (value of System.getProperty("user.dir")) which is probably not what you expect. If you call new File(pathToFile).exists() it will return false check the resulting path first.

Upvotes: 3

Related Questions