Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a Java Properties file to my Java Project in Eclipse

I was using Unix before to compile and edit my Java. In that I have used property files right inside my current working directory where the class file exists. Now i have switched to Eclipse IDE. I dont know how to add the same properties file here in Eclipse. Please help me.

like image 462
Vijay Athreyan Avatar asked Sep 23 '10 08:09

Vijay Athreyan


People also ask

How do I open properties file in eclipse?

Open eclipse. Right click on the project, select New→ file→ give file name as “config. properties”→ Finish.

How do I add to properties file?

Create a properties fileRight-click and select Add New Properties File. A new properties file will be added to your project. The new file will be selected and highlighted in the list. Type a name for your properties file, for example, "Properties".


7 Answers

  1. Create Folder “resources” under Java Resources folder if your project doesn’t have it.
    1. create config.properties file with below value.

enter image description here

/Java Resources/resources/config.properties

for loading properties.

Properties prop = new Properties();
InputStream input = null;

try {

      input = getClass().getClassLoader().getResourceAsStream("config.properties");


    // load a properties file
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("database"));
    System.out.println(prop.getProperty("dbuser"));
    System.out.println(prop.getProperty("dbpassword"));

} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
like image 126
Khalid Habib Avatar answered Oct 03 '22 08:10

Khalid Habib


In the package explorer, right-click on the package and select New -> File, then enter the filename including the ".properties" suffix.

like image 25
Michael Borgwardt Avatar answered Oct 03 '22 09:10

Michael Borgwardt


It should work ok as it is in Unix, if you have properties file in current working directory. Another option would be adding your properties file to the classpath and getting the inputstream using this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties"); More here

like image 39
Johnbabu Koppolu Avatar answered Oct 03 '22 09:10

Johnbabu Koppolu


steps:

  1. Right click on any package where you want to create your .properties file or create a new package as required
  2. now select new then select file (if you dont find file then go to location of your package and create it there)
  3. now named it as yourfilename.properties
like image 38
Aman birjpuriya Avatar answered Oct 03 '22 09:10

Aman birjpuriya


If you have created a Java Project in eclipse by using the 'from existing source' option then it should work as it did before. To be more precise File > New Java Project. In the Contents section select 'Create project from existing source' and then select your existing project folder. The wizard will take care of the rest.

like image 44
Sagar V Avatar answered Oct 03 '22 08:10

Sagar V


  1. Right click on the folder within your project in eclipse where you want to create property file
  2. New->Other->in the search filter type file and in the consecutive window give the name of the file with .properties extension
like image 37
kushi Avatar answered Oct 03 '22 09:10

kushi


To create a property class please select your package where you wants to create your property file.

Right click on the package and select other. Now select File and type your file name with (.properties) suffix. For example: db.properties. Than click finish. Now you can write your code inside this property file.

like image 39
Md Mehadi Hasan Mozumder Avatar answered Oct 03 '22 09:10

Md Mehadi Hasan Mozumder