Jarede
Jarede

Reputation: 3498

coldfusion 9 working with java objects

I'm currently working with the jodatime Java library and running into issues when trying to use it within coldfusion.

I've downloaded the latest jodatime 2.1 release, put the jar file into a folder on my local drive and pointed my coldfusion administrator to look at that folder in the ColdFusion Class Path under the Java and JVM settings page.

For the most part it works. but there are times when i get things like this:

local.oTestZone = createObject('java','org.joda.time.DateTimeZone').init('Europe/London');

Which should match with this: Constructor however I get an error in coldfusion saying:

Unable to find a constructor for class org.joda.time.DateTimeZone that accepts parameters of type ( java.lang.String ).

It works perfectly fine when I do something like this though:

local.oToZone   = createObject('java','org.joda.time.DateTimeZone').forID('Europe/London');

Which matches on: forID

Am I missing something with my java implementation?

Upvotes: 0

Views: 625

Answers (2)

nosilleg
nosilleg

Reputation: 2163

You are dealing with an Abstract Class and a Protected Constructor.

A Protected Constructor means that only a subclass or a class in the same Package can call that constructor. So even though you are supplying the correct parameter, the constructor isn't available to your code.

The ColdFusion documentation has these tidbits:

"Although the cfobject tag loads the class, it does not create an instance object. Only static methods and fields are accessible immediately after the call to cfobject."

This is why forID works; it's a static method.

"To have persistent access to an object, you must use the init function, because it returns a reference to an instance of the object, and cfobject does not."

This and the previous statement are why methods like getOffset wont work in this situation.

I'm not familiar enough with this to know if there's a class that you can instantiate that will give you access to the constructor, but hopefully someone else can chime in.

Upvotes: 2

barnyr
barnyr

Reputation: 5678

The DateTimeZone(String id) constructor is marked protected (it took me 3 reads of the JavaDoc to spot that), so CF won't be able to invoke it.

It looks to me like JodaTime expects you to use one of the static methods to construct your instances, so your second example is probably the right way of doing it.

Upvotes: 4

Related Questions