I am trying to load properties file. Here is my structure
Now i am trying to load test.properties file. But i am getting null. Here how i am doing
public class Test { String workingDir = System.getProperty("user.dir"); System.out.println("Current working directory : " + workingDir); File temp = new File(workingDir + "\\" + "test.properties"); String absolutePath = temp.getAbsolutePath(); System.out.println("File path : " + absolutePath); Properties properties = null; try { properties = new Properties(); InputStream resourceAsStream = Test.class.getClassLoader().getResourceAsStream(absolutePath); if (resourceAsStream != null) { properties.load(resourceAsStream); } } catch (IOException e) { e.printStackTrace(); } System.exit(0); } //end of class Test
This program prints
Current working directory : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration File path : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties
But it is not loading properties file from this path. Although it is present there. Why i am getting null ?
Thanks
Edit--- ----------------------------
String workingDir = System.getProperty("user.dir"); System.out.println("Current working directory : " + workingDir); File temp = new File(workingDir, "test.properties"); String absolutePath = temp.getAbsolutePath(); System.out.println("File path : " + absolutePath); try { properties = new Properties(); InputStream resourceAsStream = new FileInputStream(temp); if (resourceAsStream != null) { properties.load(resourceAsStream); } } catch (IOException e) { e.printStackTrace(); } System.exit(0); Current working directory : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration File path : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties java.io.FileNotFoundException: D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at com.softech.ls360.integration.BatchImport.main(BatchImport.java:57)
Oh oh ... There are several problems here:
1) In your first provided code snippet, you are using a ClassLoader
for loading a resource file. This is indeed a good decision. But the getResourceAsStream
method needs a "class-path relative" name. You are providing an absolute path.
2) Your second code snippet (after edit) results in not being able to find the file "D:...\LS360BatchImportIntegration\test.properties". According to your screenshot, the file should be "D:...\LS360AutomatedRegulatorsReportingService\test.properties". This is another directory.
I fear, that your descriptions are not up to date with the findings on your machine.
But let's just move to a reasonable solution:
1) In your Eclipse project (the screenshot tells us, that you are using Eclipse), create a new directory named "resources" in the same depth as your "src" directory. Copy - or better move - the properties file into it.
2) This new directory must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.
3) As the resources directory now is part of your class path and contains your properties file, you can simply load it with getResourceAsStream("test.properties")
.
EDIT
I just see, that you also use Maven (the pom.xml file). In Maven, such a resources directory exists by default and is part of the build path. It is "src/main/resources". If so, just use this.
Please put your property file in /src/main/resources folder and load from ClassLoader. It will be fix.
like
/src/main/resources/test.properties Properties properties = null; try { properties = new Properties(); InputStream resourceAsStream = Test.class.getClassLoader().getResourceAsStream("test.properties"); if (resourceAsStream != null) { properties.load(resourceAsStream); } } catch (IOException e) { e.printStackTrace(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With