10

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...

8 Answers 8

11

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");

5

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?

1
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

1
  • 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).
    – doekman
    Mar 18, 2009 at 8:11
1

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.

3
  • How do I add the config-directory to my classpath in GlassFish/NetBeans? I can't find it anywhere...
    – doekman
    Feb 12, 2009 at 12:56
  • Just put it in WEB-INF/classes directory Feb 12, 2009 at 13:12
  • Could you try ServletContext.getResource? Feb 12, 2009 at 17:23
1

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.

0

+1 for putting it in your classpath.

If you're using Spring (and I'd highly recommend you do if you're not already for many reasons) when you can load a properties file like this:

database.username=scott
database.password=tiger

and put references in your application context like:

<property name="username" value="${database.username}"/>

(assuming you've configured the property configurator) and it will cause an error if the file can't be loaded or the property doesn't exist. The application will fail to start. This is actually a good thing. It lets you find problems really really quickly and much better than failing silently, which can sometimes have catastrophic effects.

6
  • 1
    Just use spring for configuration? I'd rather use some standard things.
    – doekman
    Feb 12, 2009 at 11:04
  • 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).
    – cletus
    Feb 12, 2009 at 11:29
  • 1
    We're just creating webservices.
    – doekman
    Feb 12, 2009 at 11:32
  • 7
    I just want to read a property-file. That shouldn't be that difficult?
    – doekman
    Mar 13, 2009 at 12:09
  • 1
    Spring wouldn't bring anything new here, but non-standard dependencies.
    – ymajoros
    Apr 20, 2012 at 12:20
0

Copy your property file to the web/WEB-INF/classes path

Properties p=new Properties();
p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("myproperty.properties"));
0

Be aware that you have to close the InputStream. Otherwise you will get a SocketException sooner or later.

#|2013-xx-xxTxx:xx:xx.162+0200|WARNING|sun-appserver2.1|sun.rmi.transport.tcp|_ThreadID=431; _ThreadName=RMI TCP Accept-0;_RequestID=xyz;|RMI TCP Accept-0: accept loop for ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=42384] throws
java.net.SocketException: Too many open files
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:390)
    at java.net.ServerSocket.implAccept(ServerSocket.java:453)
    at java.net.ServerSocket.accept(ServerSocket.java:421)
    at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:369)
    at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:341)
    at java.lang.Thread.run(Thread.java:662)
|#]

Loading Properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.