• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Glassfish System Property + Relative Path

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Community,

I have recently deployed an application (myproject) within my Glassfish app server. I have a quick question. I would like to create a system property / or instance property to the relative path of a config/properties file (*.properties) located within my deployed application.

How do I specify a Glassfish property (System/Instance) to the path of the config/properties file, again, that is located within a the already deployed application (myproject)?

Thank-you kindly
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand you correctly, your properties file is inside the jar/war file. To access such file use the getClassLoader().getResourceAsStream("full path to file")
 
Marco Di Baggio
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello K.Tsang,

Your almost correct, I do want to access the properties file inside my war file, however I was hoping to simply declare a system property in Glassfish (server->system properties) as I will not be able to open up the code to change it.

Is this even possible?
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess it's possible. Just need to know the path relative to the web app.

Or if you use HTTP Apache, such file may put there instead. Reading it in code is similar (open URL or something)


EDIT: after googling, Glassfish system properties should be available through System.getProperties(). Maybe try create your system properties in Glassfish/asadmin command (eg create-system-properties mykey=myvalue) then read and display them to see if they are present.



Your output should contain lines similar to "list-system-properties" using asadmin command.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a very important difference between "files" inside a WAR file and actual files.

You cannot do filesystem open/close/etc. operations on the "files" inside a WAR file. Technically, those aren't files, they're resources. The actual access to them has to be done via the ZIPfile class methods.

What makes this even more confusing is that sometimes some webapp servers will unzip (explode) WAR files to construct an actual filesystem directory structure with true subdirectories and files. But that's not a safe assumption to make, since it's not always true and some of those systems (such as Tomcat) allow disabling that function. So to be safe, never use file operations on webapp resources. Use the getResource methods. And be aware that the get system file path method of a resource can return null if the WAR wasn't exploded and thus no physical file exists.

If my memory serves, there is an enumeration method that can be used to browse a WAR for resource paths, and that would allow you to search for, open and read (but not write!) any resource that matched your criteria (path ends with ".properties").

Additionally, if you put the resource in a jar in WEB-INF/lib or in the WEB-INF/classes resource sub-tree, you can use classpath methods to discover resources, since those locations automatically go into the webapp's classpath.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic