ResourceBundle can't find properties file unless in package

C

charles.hajj

Hello,

I am using the ResourceBundle to load properties files for i18n

I have this strcuture :

-<My Project>
-conf
-locale
-MessageBundle.properties
-MessageBundle_en.properties
-src
-com.cha.core.locale.<MyClass>

in <MyClass> I am using the following code :

ResourceBundle messages =
ResourceBundle.getBundle("conf/locale/MessagesBundle", m_locale);

As you see, I need to isolate the configuration files in the conf
directory. The properties files are now under <Working
Dir>\conf\locale.

However, getBundle() can't find them
(java.util.MissingResourceException) unless they are under the source
directory (src in my case)

Could you please help on that issue.
 
T

Thomas Fritsch

ResourceBundle messages =
ResourceBundle.getBundle("conf/locale/MessagesBundle", m_locale);
Use
ResourceBundle.getBundle("conf.locale.MessagesBundle", m_locale);
instead.
 
R

Roland de Ruiter

Hello,

I am using the ResourceBundle to load properties files for i18n

I have this strcuture :

-<My Project>
-conf
-locale
-MessageBundle.properties
-MessageBundle_en.properties
-src
-com.cha.core.locale.<MyClass>

in <MyClass> I am using the following code :

ResourceBundle messages =
ResourceBundle.getBundle("conf/locale/MessagesBundle", m_locale);

As you see, I need to isolate the configuration files in the conf
directory. The properties files are now under <Working
Dir>\conf\locale.

However, getBundle() can't find them
(java.util.MissingResourceException) unless they are under the source
directory (src in my case)

Could you please help on that issue.
Make sure that <WorkingDir> is in the classpath when you run your
application. Assuming the compiled class files are in <BinDir> (which
could be the same as your 'src' directory), the commandline to start
your app should be like this:

java -classpath <BinDir>;<WorkingDir> your.pkg.Application

[If you are running Unix/Linux, replace the separator ; in the classpath
by :]
 
C

Carlitto

Thanks to you all.

However, I don't want the workingdir to be in the classpath. Only
source files. and the output directory is different also (src, classes)
ResourceBundle loads file if they are part of the classpath.

Is there any way to load a property file outside the classpath (a
seperate conf directory in the workingdir) using ResourceBundle ? If
not, how can I load properties files for i18n other that using
ResourceNundle?

Thanks.

Charles.
 
R

Roland de Ruiter

Carlitto said:
Thanks to you all.

However, I don't want the workingdir to be in the classpath. Only
source files. and the output directory is different also (src, classes)
ResourceBundle loads file if they are part of the classpath.

Is there any way to load a property file outside the classpath (a
seperate conf directory in the workingdir) using ResourceBundle ? If
not, how can I load properties files for i18n other that using
ResourceNundle?

Thanks.

Charles.
Write your own class loader (subclass java.lang.ClassLoader) and implement
protected URL findResource(String name)
which locates the resource in your workingdir.

Then use ResourceBundle#getBundle(String,Locale,ClassLoader) with your
own class loader instance.
 
C

Carlitto

Roland,

Thanks, it worked I overrided URL findResource(String name) in my
custom ClassLoader:

protected URL findResource(String name)
{
File f = new File(name);

try
{
return f.toURL();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.findResource(name);
}

Cheers,

Charles.
 
T

Thomas Hawtin

Carlitto said:
Thanks, it worked I overrided URL findResource(String name) in my
custom ClassLoader:

protected URL findResource(String name)

Why not just use java.net.URLClassLoader.newInstance?

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top