3

I have a properties file myprops.properties as follows:

Wsdl=someurl
UserName=user
UserPassword=pasword
Application=appName

And inside my controller I'm trying to access to set values in my service as follows

Properties prop = new Properties();
prop.load(new FileInputStream("resources/myprops.properties"));
myService.setWsdl(prop.getProperty("Wsdl"));
myService.setUserName(prop.getProperty("UserName"));
myService.setUserPassword(prop.getProperty("UserPassword"));
myService.setApplication(prop.getProperty("Application"));

my Issue is I just do not know what path to use. Its a Spring project if that makes any difference. and Idealy I would like to have the properties file in my "src/main/resources" folder

I realise this may be very simple to some but I have tried searching for the solution both here and on Google and I cannot seem to find a solution that has helped. I've tried moving the file around the project but cannot seem to figure it out

The Error I get is

java.io.FileNotFoundException: resources\drm.properties (The system cannot find the path specified) 

any advice/explanation or even a link that clearly explains it would be great

jonnie
  • 12,260
  • 16
  • 54
  • 91

5 Answers5

2

well, src/main/resources are on the classpath, you just need to do.

Properties properties = PropertiesLoaderUtils.loadAllProperties("your properties file name");

1

Given that src/main/resources is on the classpath, you could do:

Resource resource = new ClassPathResource("/myprops.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I still get `java.io.FileNotFoundException: class path resource [myprops.properties] cannot be opened because it does not exist` – jonnie Feb 25 '13 at 16:23
1

If you are using spring, you could set your property placeholder.

 <context:property-placeholder location="classpath:resources/myprops.properties" />

and in your beans you can inject the values from the properteis using the @Value annotation

@Autowired
public Foo(@Value("${Wsdl}") String wsdl) {
   ...
}

in the case above I used in the constructor, but its possible to use by Autowired field/setter.

So in your service you could have something like:

@Service
public class MyService {
     private final String wsdl;
     private final String username;
     private final String password;
     private final String application;

     @Autowired
     public MyService(
         @Value("${Wsdl}") String wsdl,
         @Value("${UserName}") String username,
         @Value("${UserPassword}") String password,
         @Value("${Application}") String application
         ) {
         // set it to each field.
     }
}
Caesar Ralf
  • 2,203
  • 1
  • 18
  • 36
  • This took me a while to figure out but now that I have Definitely worth it and much more suitable then Reimeus example, Thank you – jonnie Feb 28 '13 at 14:06
0

Don't use a FileInputStream; use getResourceAsStream() to read it from the servlet context.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I tried this and get `java.lang.NullPointerException at java.util.Properties$LineReader.readLine` – jonnie Feb 25 '13 at 16:22
  • You didn't check to see if the InputStream that you got back was null. Could be that the path you gave it was wrong. – duffymo Feb 25 '13 at 16:29
0

You can always count on mkyong. This is an example/tutorial on how to load and read property files.

http://www.mkyong.com/java/java-properties-file-examples/

This question should be marked as a duplicate:

  1. Loading a properties file from Java package
  2. How to use Java property files?
  3. Load a property file in Java
  4. Java Properties File not loading
  5. Java NullPointerException on loading properties file
  6. Not able to load properties file in Java
Community
  • 1
  • 1
Jess
  • 23,901
  • 21
  • 124
  • 145