Java and ColdFusion its not that difficult.
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:
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.
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
I also made a Demo.java to test the code:
You can run the Demo.java from withing eclipse to see if you made any errors, the result will be displayed in the console:
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:
I wrote the same code as a cfc to show the comparison:
( I used CF8 so I write the components in tags )
I then made a page to create a few of each object type ( cf and java ) and show how the code is almost identical.
I then increased the loops to 10k to see the results:
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
Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.