My Own Hello World Java Class For ColdFusion
I downloaded the Java 5 SDK and tried to create a Java class. It compiled fine, but when I tried to run is using the URLClassLoader, I got the ColdFusion error:
HelloWorld (Unsupported major.minor version 49.0) null
Apparently, ColdFusion MX 7 is still running on the Java VM 1.4.2 (thanks CF-Talk!). I downloaded the Java 1.4.2 SDK from Sun and with some help from my Java-knowing co-worker, David Stamm, I easily created my first Hello World Java Class:
public class HelloWorld {
public HelloWorld(){
// Constructor code.
}
public java.lang.String SayHello(){
return(
"Hello You Beautiful World, You!"
);
}
}
Yeah, yeah, I know I don't need "java.lang.String", but I am learning, and that makes me feel more comfortable like a warm blanket. I loaded this using the URLClassLoader and everything worked super nice. Now that I know that I can do this, it's time to get some crazy experimenation going.... more on that to come :D
Want to use code from this post? Check out the license.
Reader Comments
Yes, I realized the same after writing some date/time, timezone, and locale classes at my work for our ColdFusion devs.
You the man.....
The problem I'm having with URLClassLoader is if my Java code reference other Java classes (that are part of the standard Java library) then even if those classes are in the same directory, they don't get imported.
Apparently, the Java VM is still looking in the class path set by CF to find an classes it needs to import.
So if you are going to use Java and use classes that import classes that are not part of the standard Java install on CF then URLClassLoader is not going to work for you.
Above should be ARE NOT part of the standard Java library of classes...
Bruce,
I have not even gotten there yet :) Right now, I am trying to pass in a ColdFusion component and call methods on it. Apparently that is a real pain. I am not 100% familiar with how classes are typed. ColdFusion components are of type coldfusion.runtime.TemplateProxy and that doesn't seem to allow me to call methods directly. Furthermore, I can't define any variables of that type since I don't have access to that as far as I know (via Import... I am not on a CF machine).
I am working through this slowly, but it is a hard, up hill battle.
Hey Ben,
Using java classes in cold fusion is a fantastic solution to Oh so many problems and overcomes any limitation of CF i've encountered. I've found it very easy to load classes through CreateObject. You do have to specify your class path which isn't too much of a hassle.
I use Eclipse IDE for java which allows me to set the compile version to 1.4, necessary for CFMX7. It also allows me to import external JARs and jres. I imported the coldfusion.jar and imagine the wealth of knowledge as you can peruse the coldfusion framework in its package hierarchy, although source isn't included you can see the public methods and properties as well as the super class that the folks from Allaire extended from.