7

I need to use my own java class in a cfml page.

This entry in the documentation sounds great but does not explain which files I have to create.

I tried to create a test.cfm page under my website root. Then placed TestClass.java + TestClass.class in the same path. But that results in an error "class not found"!.

Can you please help me?

Leigh
  • 28,765
  • 10
  • 55
  • 103
Fabio B.
  • 9,138
  • 25
  • 105
  • 177

1 Answers1

14

a TestClass.java + TestClass.class in the same path.

You cannot just place .class files anywhere. When the CF server starts, it only checks specific locations for classes/jars. Those locations are referred to as the "CF class path". Your compiled .class file must be placed within the CF class path, or it will not be detected.

To use a custom java class:

  1. Create a source file ie YourTestClass.java
  2. Compile the source code into a class file ie YourTestClass.class
  3. Place the compiled .class file somewhere within the CF classpath, such as:

    • WEB-INF\classes - for individual .class files
    • WEB-INF\lib - for .jar files (multiple classes)

    Note: You could also add the item to the CF class path via the ColdFusion Administrator. However, placing the class in one of the default directories is simpler.

  4. Restart the ColdFusion server so it detects the new classes

Note: Though you can use individual .class files, it is more common to package them into .jar files.

Leigh
  • 28,765
  • 10
  • 55
  • 103
  • As a side note, you can also create a new folder in cf and add the class path in the administrator if you want to keep you custom classes seperate. – Dpolehonski Sep 07 '12 at 08:50
  • You can do dynamic loading if you use JavaLoader ( http://javaloader.riaforge.org/ ) or ColdFusion 10 (which includes JavaLoader). – Tony Miller Sep 11 '12 at 13:02
  • True, once you get comfortable with the basics of compiling and using classes you can take advantage of cool features like dynamic class loading or modifying the class path. (I would strongly recommend using jars, rather than individual class files too. But one step at a time ;) – Leigh Sep 11 '12 at 13:07