Ok, this is nothing new. In fact, this isn't even for YOU. This is for me. This is coming right off of Spike-Fu's blog entry titled "Loading java class files from a relative path." He wrote it back in 2004; I am simply rewriting it here in my own words so that I can explain it to myself and see that it actually works and see if actually understand what the heck is going on. It's a post that I have know about for a long time but have just never taken the time to get into it. Now is that time.
If you follow my blog closely, you will know that I don't like things like class paths, path mapping, or basically, anything that requires the need to have special permissions on the hosting machine (ie. access to the admin or secured file system). Obviously, data sources are required, but that is once per project. I hate the idea of having to go back to the ColdFusion admin for much of anything. I like my projects to be very module. I like the idea of picking it up, dumping it somewhere else and just having it WORK. I know this creates duplicate code (for all you framekwork people) but I am sooo OK with that it's not even funny.
The ability to be able to load Java class files without them being in the ColdFusion list of Java class paths is something that fits into this ColdFusion application development mentality.
Ok, so let's look at the example. I downloaded Spike's HelloWorld.class file and have placed it in the same directory of the calling code. Then, I loaded it and called a method on it to see that it was working.
Launch code in new window » Download code as text file »
This outputs the phrase "Hello World!" It works quite nicely and very fast, at least on this tiny example. Still, very very cool that you can load Java classes without having to mess with the Admin, which you know I think is highly sexy.
Just a few notes. You will see when we load the class, we then call the Java method NewInstance() on the class. I don't fully understand how all this works, but I guess the first part just loads the definition for the class then the new instance call loads a new instance of that class. If you look at the Java 2 documentation, you will see that NewInstance():
"Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized."
This is very interesting. If you use new instance, you cannot pass in an initialization list. That's good to know for future use (and experimentation).
Download Code Snippet ZIP File
Comments (3) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
or just use the JavaLoader.cfc (http://www.compoundtheory.com/?action=javaloader.index) ...
Posted by Rob Gonda on Oct 8, 2006 at 12:04 PM
Rob,
I appreciate the link. It looks like the JavaLoader is doing the same thing that Spike was, just in a more packaged object. But, my intent was not so much to be able to do it, but more to understand how it worked and just to explore that it was indeed possible.
Thanks.
Posted by Ben Nadel on Oct 8, 2006 at 12:52 PM
And it's a great explanation and makes total sense, thanks!
Posted by Rob Gonda on Oct 8, 2006 at 1:10 PM