Java and ColdFusion ( well CFML including OpenBD and Railo ) work really well together as you would expect. However there is not a lot of information out there about exactly how to create (or use) existing Java within ColdFusion, which is whyy I decide to make a simple example and a step by step post of how to create java code and execute it from ColdFusion.

All the files use are available for download at the bottom of the post.

You don't need alot of tools. Initially I created the whole demo with notepad++ and compiled the .java files from the CLI but its easier to manager if you use eclipse. The only real component you need is a jdk you can confirm you have this installed by running the following:
javac -version

notice its javac not java as we need to be able to compile .java code. I completed the example on Windows but it will be the same on any operating system.

javaVersion

If you do not have a JDK installed you can download it here Sun JDK

Launch Eclipse, create a project and right click src and select “new” and then “package”. Create com.transport

This is the package structure for this example. In transport you then make 3 new class's Vehicle.java, Car.java and Bicycle.java

eclipse

Vehicle.java:

javaVehicle

Car.java:

javaCar

Bicycle.java:

javaBicycle

I also made a Demo.java to test the code:

javaDemo

You can run the Demo.java from withing eclipse to see if you made any errors, the result will be displayed in the console:

javaConsoleOutput

You can now export the code as a jar.

To extract to a jar to use with ColdFusion, Railo or OpenBD:

Select the .java files you want ( Vehicle, Car, Bicycle ) right click and select “Export” , expand “Java” and select “Jar file” you can export this to any location and jar name. As I have ColdFusion running locally I exported directly to c:\ColdFusion8\lib as paul.jar

( on railo this location is: /var/www/virtual/sitename.com/WEB-INF/railo/lib/ basically the /WEB-INF/railo/lib/ under your webcontext you want access to the jar )

You need to restart ColdFusion to pick up the file.

One CF restarts it should have access to the java class’s we just made:

Create a new .cfm page and we can now create java objects of the classes we just made directly from ColdFusion:

cftojavamethods

The output:

cftojavamethodsresults

I wrote the same code as a cfc to show the comparison:
( I used CF8 so I write the components in tags )

Vehicle.cfc:

cfVehicle


Car.cfc:


cfCar

I then made a page to create a few of each object type ( cf and java ) and show how the code is almost identical.

javacfspeed

I then increased the loops to 10k to see the results:

cfVJavaObjects

Yes java objects are much faster then ColdFusion.

The point of the post is to show how easy it is to jump between java and CF and if you have algorithms, class's or any code that's already built in java you can easily leverage this in ColdFusion.

Also note that I compiled the code on Windows 7 64 bit and tested on ColdFusion 8 I then dropped the jar onto my linux VPS running Railo without a single change in code or recompiling the java code.

Here is the speed test running on ColdFusion9 under Ubuntu: http://demo.kukiel.net/java

Here are the files: DemoFiles.Zip
I have said it before and I'll say it again. If you're not using some type of source control you should be.

SVN makes life easy. I'm the type of person that likes to run the latest Betas (Win7 for one) and that often means I reformat and reinstall my Operating System regularly. I also jump between Linux, Windows and OSX.

One of the hardest things to move between machines is my development environment and making sure I have my code and don't lose anything. We use SVN at work and I use SVN for all my personal projects its just easy to check out my code and continue working.

I have recently moved my projects to Project Locker as they have free accounts with up to 500mb storage, SSL plus more.

If you're not using SVN then you should really check this out and start.

I still get asked about installing ColdFusion 8 on Windows 7 and Vista from time to time I remember when Vista SP1 came out and ColdFusion would no longer just "install" like it should and it took a bit of tinkering to come up with the solution. Dale Fraser worked this out for us and it works perfectly on Vista and Windows 7 and Windows Server 2008. This is also the same process for ColdFusion 9.

A little while ago I also created a step by step walk through and here it is: Installing ColdFusion on Windows 7 </ a>

Once that is setup you might like to create addition HOST entries so you access your sites locally via host name rather then http://localhost/site1
Steve Withington has a walkthrough on the process here.

There is sound to this as I step through the process.

If you are interested in Windows Server 2008 and ColdFusion 9 install here is a walk through: http://vc.kukiel.net/cf9win2k8

cfmail is script is easy but as the docs are still a bit lacking and there is no mention of the attributes for using mail in script I had a look in the CF9 install directory and in

/opt/ColdFusion9/CustomTags/com/adobe/coldfusion

You will find the cfc's Adobe made to extend script support. I opened up the mail.cfc and worked out how to use the function. Here is a small example.

Of course to, from, subject etc can be strings they don't have to be variables.
ColdFusion 9 is now officially supported on Ubuntu 9.04 I think this is great news. For a while we knew that it did work really easily on Ubuntu and Debain but it's now good to know its officially supported. You can see Adobes ColdFusion 9 Requirements here

Ubuntu is great but for some simple testing is can be quite heavy. If you have been thinking about trying ColdFusion 9 on Linux I might recommend Debian. I like the Debian net install because it's a minimal install of a basic system. The download is ~150meg and installs lean and quickly.

I used VMware EXSi on an old spare PC but Virtualbox, VMware server or Parallels will also work if you don't have a spare machine. Create a new Virtual machine and allocate 512mb ram. For testing and trying the new features this will be totally fine to run the slim Operating system and ColdFusion 9 even mySQL. When you start configuring your virtual machine make sure the network is set to "Bridged" so its connected directly to the physical network so it gets a real IP address from your DHCP server or router.

After the install there are a few commands and updates I usually run:

#Update the apt repository's
apt-get update

#insall some additions to VI ( simple text editor ) that make is more
#usable on Debian
apt-get install vim-nox

#install a ssh server so we can connect over putty and over sftp with ColdFusion #builder
apt-get install ssh

#you may wish to install apache even if you don't its impressive how easy it is
#with apt
apt-get install apache2

Run ifconfig ( if not ip as in windows ) to find the ipaddress ( presuming you selected dhcp )

At this point I drop out of a direct console session and continue the install with putty.

Download the linux .bin file ( 64 or 32 bit depending on the edition of debian you choose )

Use scp to move the file across. As my main machine is currently running Windows 7 I use CoreFTP which has scp support. You can just drop the .bin file to the root home directory.

Start up putty and ssh to the ip of the VM and log in.

The rest of the install I made a short screen capture pausing out the delays.



Once ColdFusion is installed and started you can test it out. In this short screencast I complete the install and log into the ColdFusion admin and setup a connection over SFTP in ColdFusion Builder create a test file and make sure CF9 is working.



This is a simple testing environment so I paid no attention to security and simply logged in as root. This is a simple demonstration how in less then half an hour you can have a slim OS running ColdFusion 9 in linux and edit your files from ColdFusion Builder. This example also uses the inbuilt ColdFusion webserver rather then connecting to apache which is also an option as part of the CF9 install.


Added an entry on installing mySQL here: