5

I need to read the configuration details from a properties file in eclipse. I have put the config.properties at the same level as plugin.xml and in the class file I call:

Properties properties = new Properties();
FileInputStream file;
String path = "./config.properties";
file = new FileInputStream(path);
properties.load(file);

I get a file not found exception. Is there a better way of doing this?

Gangnus
  • 24,044
  • 16
  • 90
  • 149
user1688404
  • 729
  • 3
  • 9
  • 19

3 Answers3

5

Did you remember to include it in the build?

Secondly, using the classloader resource is probably better anyway

InputStream fileStream = myClass.getResourceAsStream( "/config.properties" );

Also, there is another way of opening a resource URL in eclipse using

url = new URL("platform:/plugin/com.example.plugin/config.properties");
InputStream inputStream = url.openConnection().getInputStream();
Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
1

Put the properties file in the root of your project. This should be where the user.dir system property is pointing. The FileInputStream constructor looks in this directory for the file.

You can confirm it is in the correct directory by outputting the System Property.

System.getProperty("user.dir");
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • These easy methods won't work for plugins. There you have two or even three-storey folders system. (plugin/[resourceFolder/]localPath). https://stackoverflow.com/a/56327069/715269. And the file should be present in the target, too. – Gangnus May 31 '19 at 09:55
0

If it is a plugin, you should tell it that your file must be included into the target. It is set in the build.properties file. Open it and look - you haven't your file mentioned there.

Open plugin.xml, go to build tab and include your file in the binary.build by checking it. Save. That will change the build.properties file correctly. Go to build.properties tab and look at the file again - now you see your file included correctly.

Of course, you could do it by hand, but plugin UI is convenient and you can be sure you haven't any problem ends of line or spaces.

For absolute addressing the files in the plugins look here

Gangnus
  • 24,044
  • 16
  • 90
  • 149