Tuesday, November 21, 2006

Java CFX and Jar Hell

While this topic may not come up all that often, it came up recently for me. I started to play around with building Java CFX tags for ColdFusion to add in some functionality. While I could have just invoked the java code within CF, I chose to try learning how to build a CFX tag. So first off I went with the absolute basic - the Hello World Application:
import com.allaire.cfx.*;
public class HelloWorld implements CustomTag
   public void processRequest(Request request, Response response) throws Exception
      response.write("Hello World");

Now comes the compiling part. This was a little tricky, but the only key to this is knowing that you have to include the classpath to your cfx.jar file in order for it to compile properly. The following example assumes javac is in your path and you are in the same directory as HelloWorld.java:
javac HelloWorld.java -classpath "C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\lib\cfx.jar"

Once that compiles properly put it in the "C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\ classes" folder.

Now register it within the CF Administrator by going to the Extensions -> CFX left navigation and choose "Register Java CFX". Then make the tag name CFX_HelloWorld and make the Class name "HelloWorld" (sans quotes and Case Sensitive based on the name of your class file you just compiled)

To call this on a .cfm page use:

(not case sensitive). That's it!!! However I started to think - hmmm applications can have more than one class file, so normally I would just jar the files. Simple enough in theory. HOWEVER, after spending several hours trying to get a JAR to work by putting it in various areas (classes folder etc and even adding it to the JRun classpath) I had to resort to asking for Macrobe (it's hard to get away from Macromedia and calling them Adobe) help through their support line. They were very helpful and in one simple e-mail they said that all you have to do to get a jar to work is put the jar in "C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\lib" and restart the server. Registration within the cfadmin is the same for the class. For my simple example I took the same HelloWorld class file and jar'd it. Then I named the jar the same as the class file and put it in the lib folder. After a restart, it worked! Whew - I wish I knew that many hours ago. Just for a little background - I was using CFMX 7.0.2 with JRun 4 with the JDK 1.4.2_12 (the version of the Java SDK should match the JVM version of your ColdFusion Server)

Hopefully my little walk around the park will help someone else out since Google didn't seem to help this time. Good Luck!

No comments: