Domanda

i put the urls.properties in conf folder and in src/java folder, but when i want to read this file from java class in grails project, i get an error: java.io.FileNotFoundException: urls.properties (The system cannot find the file specified).

I try read this file with this code:

io = new FileInputStream("urls.properties");
prop.load(io);
url = prop.getProperty(urlName);

what is wrong and why i get this error? I have a conf parameter in Config.groovy as

grails.config.locations = ["classpath:urls.properties"]
È stato utile?

Soluzione

See this chapter in the manual for information about loading external config files: http://grails.org/doc/2.3.7/guide/single.html#configExternalized

After having specified a property file with grails.config.locations, the properties are loaded automatically when your application is started so they should already be accessible by in the grailsApplication.config object like this:

def grailsApplication // Injects the grailsApplication object to your service/controller
...
grailsApplication.config.myproperty 

Read more here about how to access config properties: http://grails.org/doc/2.3.7/guide/conf.html

What you are trying to do with your code is to load a file from the current working directory and probably this file does not exists there. Hence the error code.

EDIT

If you want to load a file that exist in the classpath you should load it as a resource instead. Like this:

        InputStream is = MyClass.class.getResourceAsStream("urls.properties");

...where MyClass is a class which is located in the same package as urls.properties. Don't forget to check if the stream is is null, since it will be if it fails to load the resource.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top