Blog

22Aug
2006
Accessing Java Properties

An app I am working on needs to check the Java CLASSPATH (I am trying to return more useful errors if a call fails). The CLASSPATH is a Java property (kind of like environment variables) named java.class.path, and to obtain this value all you need to do is call getProperty() in java.lang.System. To make this simpler I through these two UDFs together, the first returns a structure of all Java properties and the second returns a specific property.

view plain print about
1<!--- Return all Java properties --->
2<cffunction name="GetJavaProperties" returntype="struct" output="no">
3    <cfreturn CreateObject("Java", "java.lang.System").getProperties()>
4</cffunction>
5
6<!--- Return a specific Java property --->
7<cffunction name="GetJavaProperty" returntype="string" output="no">
8    <cfargument name="property" type="string" required="yes">
9    <cfreturn CreateObject("Java", "java.lang.System").getProperty(ARGUMENTS.property)>
10</cffunction>

It is worth noting that to access a property with a . in its name you'll need to use struct["member"] syntax instead of struct.member. So this will work:

view plain print about
1<cfset p=GetJavaProperties()>
2<cfoutput>
3#p["java.class.path"]#
4</cfoutput>

But this won't:

view plain print about
1<cfset p=GetJavaProperties()>
2<cfoutput>
3#p.java.class.path#
4</cfoutput>

Comments (12)



  • Ben Nadel

    This is not 100% relavent, but I am getting more into Java under CF and I am really excited to be playing around. Just the other day I made a UDF that gets a query of Java Class methods and constructors of a ColdFusion object... it's a work in progress, all experimental, but this Java stuff is really neat. http://bennadel.com/index.cfm?dax=blog:206.view

    Actually, you are probably THE guy to ask... all the Java hooks that we have, are those officially supported by ColdFusion? I know the serviceFactory used to be secret, and now its sort of advertised... does that go for all underlying, accessible java stuffs?

    #1Posted by Ben Nadel | Aug 23, 2006, 05:18 PM
  • Ben Forta

    Ben, those are two different questions. Core Java stuff is not going away, we have no plans on rebuilding CF in something other than Java, so leveraging underlying Java is perfectly safe (and highly reccomended). As for CF internals that are written in Java, well, if they are undocumented then they are not supported and may indeed change or evolve - and service factory definitley falls into that category.

    --- Ben

    #2Posted by Ben Forta | Aug 23, 2006, 07:26 PM
  • Ben Nadel

    Ok, I can understand stuff that is not documented... but what about stuff that is exposed via the GetMetaData() method. Are those things considered "official" or is "official" limited to what is in LiveDocs?

    #3Posted by Ben Nadel | Aug 24, 2006, 01:00 PM
  • Ben Forta

    Ben, I guess that would depend on the object. But I think you're safe with that one.

    --- Ben

    #4Posted by Ben Forta | Aug 24, 2006, 01:05 PM
  • Ben Nadel

    Ben,

    I am pretty sure I know the answer to this one, but I just wanted to run it by you. I have discovered that if i have a String object (ex. strTest = "ben forta") then I can actually call the Java replace methods directly:

    strTest.ReplaceAll( [regexp], [replacestring] )

    This however is not documented (that I can find). However, it can be found via the GetMetaData() and then a few other methods calls. So, I assuming that this is NOT safe to do, but I was hoping maybe it was :)

    #5Posted by Ben Nadel | Aug 28, 2006, 09:02 AM
  • Ben Forta

    Ben, simple CF variables are in fact Java strings, so you can can safely use any String class methods on them. FYI, you can always look at #var.getClass().getName()# to see what the underlying class behind a ColdFusion variable is.

    --- Ben

    #6Posted by Ben Forta | Aug 28, 2006, 09:08 AM
  • Ben Nadel

    Ben,

    That is very exciting... I love using the Java ReplaceAll() method :) Sweeet. And yeah, I have found some interesting stuff using thet GetClass().GetName() stuff. For instance, the underlying query object is coldfusion.sql.QueryTable and that has some cool methods like IsFirst(), IsLast(), and RemoveRows() which I would be excited to use. Do you think that is safe (and that's my final question)?

    #7Posted by Ben Nadel | Aug 28, 2006, 09:19 AM
  • Ben Forta

    Those internal query object methods are how CF works its magic (how a cfoutput over a query works, for example). I guess those could change, but I highly doubt that would happen.

    --- Ben

    #8Posted by Ben Forta | Aug 28, 2006, 09:37 AM
  • Ben Nadel

    Sweeeeeet. Thanks for all the clarification.

    #9Posted by Ben Nadel | Aug 28, 2006, 09:41 AM
  • Red sea real estate

    I read this article and I just to say thanks

    Red sea real estate
    datamaster@mysharm-el-sheikh.com
    www.mysharm-el-sheikh.com

  • sharm el sheikh investments

    I think that I have to say thanks after reading an article like this!!!

    Red sea real estate
    info@redsearealestatesharm.com
    http://www.redsearealestatesharm.com

  • celebrity news

    Absolutely right comments means suggestion of some words but some people write a article bigger than the orignal

    post.