Link to home
Start Free TrialLog in
Avatar of prashant_it
prashant_it

asked on

Java: Unable to access a properties file from a jar.

Hi
 
We have an application which has a Agent.properites file. This file is in com.pack2.pack3 package.
We have made a jar file out of the application excluding the abc.properties file since the properties file needs to be modified externally.
 
For excecution of the project.
The jar file and the properties file have been put in the same folder (say JavaApp).
Then the jar file is run. We use ResourceBundle to read the properties file. [ ResourceBundle bundle = ResourceBundle.getBundle ( "com.pack2.pack3.Agent" ) ; ]
But the applcation is not able to find the properties file.
 
Alternatives tried
1) We have tried creating the same package structure com\pack2\pack3\ within the JavaApp folder and put the Agent.properites file in it
    ( JavaApp\com\pack2\pack3\Agent.properties). Still it is not able to find the properties file.
 
2) Also we put the path of the jar file in classpath, this also did not work.
 
Please tell us what could be the problem.

Thanks
Prashant.

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

ResourceBundle bundle = ResourceBundle.getBundle ( ".com.pack2.pack3.Agent" ) ;

and make sure JavaApp is in the classpath
One option is to use Properties.load (), though I'm not sure why ResourceBundle would not work.
Instead of
   ResourceBundle.getBundle ( "com.pack2.pack3.Agent" )
Try
   ResourceBundle.getBundle ( "abc" )
 and use        java -cp <dir>;<dir>/<app>.jar

   where <dir> is the directory where abc.properties file is located
Or if you have      JavaApp\com\pack2\pack3\Agent.properties
->
         ResourceBundle.getBundle ( "com.pack2.pack3.Agent" )

         java -cp JavaApp;JavaApp\<app>.jar <mainclass> ...
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perhaps you could try the following:

    public ResourceBundle loadProperties(Class clazz, String fileName) throws IOException {
        int index = clazz.getName().lastIndexOf('.');
        URL url = clazz.getResource(clazz.getName().substring(index >= 0 ? index + 1 : 0) + ".class");
        Pattern pattern = Pattern.compile("jar:(.+)/.*\\.jar!.*");
        Matcher matcher = pattern.matcher(url.toString());
        ResourceBundle bundle = null;
        if (matcher.matches() && matcher.groupCount() > 0) {
            URL resource = new URL(matcher.group(1) + "/" + fileName);
            InputStream in = resource.openStream();
            bundle = new PropertyResourceBundle(in);
            in.close();
        }
        return bundle;
    }

What this does is first get the path used by the ClassLoader to load the current Class. This should look something like:
   jar:file:/[Windows Drive:/]dir1/dir2/dir3/application.jar!/com/pack2/pack3/MyClass.class
   
To generate a path to the directory where the application.jar is, we then apply a regular expression with a capture group that contains everything from "file:" to the last character preceding the last "/" preceding the "!". :-o  To get the URL to the desired file, we then append a "/" and the name of the properties file. We can then open an InputStream to that URL and use it to construct a PropertyResourceBundle.

Assuming the class that reads the properties file is in the jar, you could put the loadProperties method in that class and then call it this way:
    ResourceBundle bundle = this.loadProperties(this.getClass(), "Agent.properties");
   
Regards,
Jim Cakalic
I think my comment was useful too, as a matter of fact.