Java and ColdFusion its not that difficult.

by 4:53 AM 18 comments
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

18 comments:

  1. Why is all the code done as images, thats crap, I can't copy and paste any of it.

    ReplyDelete
  2. It's alot of work to make it look correct with the syntax highlighter. All of the code is available for download at the bottom of the post.

    ReplyDelete
  3. What to do if javac -version returns:

    "Que? Don't know what to do with that...[pseudo-answer]"

    ReplyDelete
  4. You may not have the JDK installed and the path to the bin folder in the java install added to your system path. You can get the JDK here: http://java.sun.com/javase/downloads/index.jsp

    ReplyDelete
  5. Thanks mucho for this. Great simple example.

    ReplyDelete
  6. nice job Paul! i'm a java newbie and have been using CF for awhile now, so this will definitely come in handy. you're absolutely right, there just aren't enough of these examples out there yet. thanks for sharing!

    ReplyDelete
  7. You can also leverage Mark Mandel's JavaLoader to load Java libraries in CF:
    http://www.compoundtheory.com/?action=javaloader.index

    ReplyDelete
  8. This is probably the most straightforward example of this I've seen. Java use to scare me, but as I've learned more and more about development with AS3, Java seems so much easier. I definitely have situations where doing things in Java would make more sense.

    Thanks!

    ReplyDelete
  9. Thanks for the post. I have been hearing for years about what Java and CF can do together, but have not seen a great deal of coding examples. This was a good example of how you can go from Java development to CF development pretty quickly. I would like to see more post like this one from you and hopefully other CF bloggers. Hmmm, maybe it's time for me to start blogging on the topic :-).

    Thanks,

    JW

    ReplyDelete
  10. Good article, I use JAVA a fair amount in a couple of our projects, I would always Advocate the use of Mark's JavaLoader component as you don't have to actually install the library onto the server, it's a much better approach and more maintainable, especially if you're on a shared host.

    Rob

    ReplyDelete
  11. Nice post Paul, very nice example

    ReplyDelete
  12. Very helpful and good example to start working with java and CF.
    But confused with the time taken by both approach.

    ReplyDelete
  13. Maybe I'm doing something wrong but I could not Test it, well, I need the classes in .class or .java for run with CF?

    ReplyDelete
  14. You need to create a jar file and drop the jar into the lib folder ( in my case C:\ColdFusion8\lib ) then restart ColdFusion.

    ReplyDelete
  15. Thanks Paul that was really great and easy, create just pakages and export the JarFiles, copying into ColdFusion/lib. Became really fast cuz we have the compiled source and doesnt need to load and make a compilation... That's really good...

    Thanks again!

    ReplyDelete
  16. This approach will use system java compiler or cold fusion in build compiler.

    ReplyDelete
    Replies
    1. It would use the jmv you specify for ColdFusion. Please not I have not used ColdFusion for 5 or so years.

      Delete