Quick example of Java via ColdFusion - Reading FLV Metadata

This post is more than 2 years old.

Earlier this week a reader asked if there was a way to read FLV Metadata via ColdFusion. There isn't anything built in (that I know of!) so I did a quick search for a Java solution. I think people forget how easy it is to use Java via ColdFusion. Even if you have no intent, or care, to read FLV Metadata, please read on as the general technique is something I've done many times in ColdFusion, and could be helpful to anyone looking to do something not directly supported via CFML.

My search for a Java based solution came to this blog post: Java program to fetch FLV metadata The author kindly provided a simple Java program that provided everything necessary.

I took his code, saved it as FLVMetaData.java. I removed the main() method because, as far as I knew, that is only useful for command line programs. It probably would have worked fine as is - so you may want to pretend I didn't say that. Once I had a class file, I needed to put it somewhere ColdFusion would recognize. I know there is a folder that ColdFusion uses out of the box, but as I always tend to forget that folder, and because I'd rather store my custom stuff separately, I added a new folder in the ColdFusion Admin:

Last but not least - I restarted ColdFusion and wrote one line of code to test that I could load the class.

<cfset ob = createObject("java", "FLVMetaData")>

And that was it! It just plain worked. Ok, so I did actually try to read some FLV metadata:

<cfset ob.init("/Users/ray/Desktop/Life-on-Repeat.flv",false)>

<cfdump var="#ob#">

<cfoutput> duration=#ob.getDuration()#<br/> width=#ob.getWidth()#<br/> height=#ob.getHeight()#<br/> </cfoutput>

and it worked perfectly. So, not rocket science of course, but again, I wanted to remind people how easy it is to extend ColdFusion with Java. I think this is a great example where something not provided by ColdFusion directly was easy enough to add with some Java code.

Example output

p.s. Why didn't I use JavaLoader? As far as I know, JavaLoader only works with JAR files. I spoke with Mark earlier this week though and he said that class files should also work (but he wasn't 100% sure).

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Luke posted on 7/2/2009 at 12:51 PM

hi Ray,

great article - using underlying java is something i've only ever dabbled in once before (to get ssl + gmail pop3 working) and to be honest it was a cut n' paste job without much thought into the possibilities it might open up.

i'd be keen to see more snippets of useful java stuff for CFers.

thanks again!

Comment 2 by Martijn van der Woud posted on 7/3/2009 at 11:46 PM

@Ray @Mark I can confirm that JavaLoader does indeed work with class files. (It works in my Railo install, haven't tested on Adobe CF).

Comment 3 by David Wall posted on 11/25/2009 at 4:29 AM

@Ray @Martijn, couple things, thanks for this article...it helped me go a direction that was previous a dark, black hole. I have two problems, when I put this code into a CFLOOP, it will not work. Next is on many of the files the obj.getWidth() reports a "-1", I'm using CF8 enterprise.

Comment 4 by David Wall posted on 11/25/2009 at 4:37 AM

Please never mind about the 2nd issue. I'm looking closer at the class file and noticed the comments. Duh... I'll continue testing, please delete these two posts if you get around to it.

Comment 5 by Amir posted on 1/8/2010 at 4:43 AM

This indeed showed me the right direction. I have a question though. Sometime inorder to create an object from a java class I have to use $ insread of "." (dot) for example:
createObject("java", "javax.mail.Message$RecipientType").BCC;


see: http://java.sun.com/j2ee/sd...

Comment 6 by Amir posted on 1/8/2010 at 4:52 AM

You can find the answer to my last post here: