How to use a property-file with GlassFish

29,156

Solution 1

The solution that works is actually pretty simple:

URL url =  this.getClass().getResource("/package/name/file.properties");
p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));

Why didn't anybody come with this?

Solution 2

Place your property files in the <glassfish-install-dir>/glassfish/domains/<domain-name>/lib/classes directory and they will be accessible from within your applications via the ResourceBundle class. For example, add a property file named settings.properties to this directory and then access values from the file like this:

ResourceBundle.getBundle("settings").getString("my-property-key");

Solution 3

Alternatives:

Depending on how your domain is configured, you might be able to use asadmin create-system-properties from the command line. Run/see asadmin create-system-properties --help for more info.

Or you might like administering system properties through the Glassfish admin interface. Here's the default link: http://localhost:4848/configuration/systemProperties.jsf?configName=server-config

Solution 4

I've tried a lot, but I solved this with:

        // ServletContext ctx
        InputStream stream = ctx.getResourceAsStream("version.properties");
        p = new Properties();
        p.load(stream);

I have to pass the ServletContext from a jsp-page with a call to getServletContext()getServletContext(). Not ideal, but it's the only way I could get it working...

It would be nice though if anyone could come up with another solution, that could work withyout the ServletContext.

Solution 5

See here for how you can read a properties file from your classpath:

URL url =  ClassLoader.getSystemResource("test.properties");
Properties p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));

You then only need to add your config directory to the classpath.

If you have problems using the above code try ServletContext.getResource.

Share:
29,156
Jon Gretar
Author by

Jon Gretar

I'm both a Mac and Windows user. I like programming web applications. My languages of choice are Python, Javascript and C#.

Updated on July 09, 2022

Comments

  • Jon Gretar
    Jon Gretar almost 2 years

    I'm creating a web service, which run in GlassFish, and I want to have some custom properties. For this I'm using the Properties class. The code I'm using is:

    Properties p=new Properties();
    File f=new File(System.getProperty("user.dir"), "settings.properties");
    p.load(new FileInputStream(f));  
    

    But how do I get the settings.properties-file in my config directory?

    I'm not sure about my classpath, since this is managed by NetBeans and GlassFish. I assume my .war-file is added to the classpath, when deploying...

    I've added my own solution, but if anyone could come up with a better solution, it would be highly welcome...

  • Jon Gretar
    Jon Gretar about 15 years
    Just use spring for configuration? I'd rather use some standard things.
  • Lordn__n
    Lordn__n about 15 years
    That's not all that you use Spring for (obviously). Particularly for Web applications running in application server, I can't envision a scenario where I wouldn't use Spring. After the JDK it has to be the second most commonly deployed jar(s).
  • Jon Gretar
    Jon Gretar about 15 years
    We're just creating webservices.
  • Lordn__n
    Lordn__n about 15 years
    Spring has extensive implementation helpers for creating Webservice endpoints and consumers.
  • Jon Gretar
    Jon Gretar about 15 years
    How do I add the config-directory to my classpath in GlassFish/NetBeans? I can't find it anywhere...
  • NikolaDjokic
    NikolaDjokic about 15 years
    Could you try ServletContext.getResource?
  • Jon Gretar
    Jon Gretar about 15 years
    I just want to read a property-file. That shouldn't be that difficult?
  • Jon Gretar
    Jon Gretar about 15 years
    That's nice to know, and might come in handy. I'd like to have some settings outside my container. But the version-number needs to be inside the container, so I want to use a property-file. Advantage: the version number is also updateable by the build script (ant).
  • matbrgz
    matbrgz over 12 years
    Is that your config directory?
  • ymajoros
    ymajoros about 12 years
    Spring wouldn't bring anything new here, but non-standard dependencies.