My Own Hello World Java Class For ColdFusion

Posted October 9, 2006 at 1:53 PM

Tags: 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:

 Launch code in new window » Download code as text file »

  • 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

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Reader Comments

Oct 9, 2006 at 5:03 PM // reply »
19 Comments

Yes, I realized the same after writing some date/time, timezone, and locale classes at my work for our ColdFusion devs.


Oct 9, 2006 at 5:10 PM // reply »
3 Comments

You the man.....


Oct 9, 2006 at 5:55 PM // reply »
3 Comments

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.


Oct 9, 2006 at 5:56 PM // reply »
3 Comments

Above should be ARE NOT part of the standard Java library of classes...


Oct 9, 2006 at 6:06 PM // reply »
6,516 Comments

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.


Aug 16, 2007 at 4:26 PM // reply »
4 Comments

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.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »