ColdFusion Compiler
The ColdFusion Compiler compiles ColdFusion templates (CFM, CFC, and CFR) into Java bytecode. Macromedia provides a command-line interface to the compiler (cfcompile.bat on Windows; cfcompile.sh on Unix). The script is located in the cf_root/bin (server configuration) or cf_webapp_root/WEB-INF/cfusion/bin (J2EE configurations) directory.You can use this utility for:
- Converting ColdFusion source code into Java bytecode for sourceless deployment of ColdFusion templates
- Precompiling ColdFusion templates into Java class files
The basic syntax for calling the cfcompile script is as follows:
It is preconfigured with the options necessary to compile source code into Java bytecode for sourceless deployment, and for compiling ColdFusion templates into Java class files.
cfcompile [-deploy] webroot [directory-to-compile] [output-directory]
Compiling Sourceless Templates
The capability to deploy sourceless ColdFusion templates is one of the most anticipated Cold Fusion MX 7 features. Sourceless means you can deploy your ColdFusion templates (CFM, CFC, and CFR files) as Java bytecode and distribute them without changing the template names or paths. The templates will contain unreadable Java bytecode but will run as if they contained CFML. This makes source code distribution more secure and protects the intellectual property contained in the files. For more information on the security of sourceless deployment, see the "Encrypt ColdFusion Templates" section of Chapter 10.Use the cfcompile utility with the deploy option to convert the source code of your ColdFusion templates to Java bytecode. The utility will make a copy of your original template and compile the CFML to Java bytecode. The template containing the Java bytecode will be written to a specified destination directory, leaving unchanged your original templates containing CFML. The cfcompile executable is located in the cf_root\bin directory. Use the following command to compile your templates to Java bytecode:
Table 6.4 describes the cfcompile parameters for creating sourceless templates.
cfcompile -deploy webroot directory-to-compile output-directory
OPTION | DESCRIPTION | REQUIRED |
---|---|---|
webroot | Specify the fully qualified path to the web server root directory (for instance, C:\CFusionMX7\wwwroot or C:\Inetpub\wwwroot). | Yes |
directory-to-compile | Specify the fully qualified path to the directory containing the templates to be compiled. This directory must be under the specified webroot directory. If omitted, ColdFusion will compile all ColdFusion templates in the specified webroot directory. | Yes |
output-directory | Specify the fully qualified path to the destination directory that will contain the compiled deployable files. This cannot be the same directory as the source directory. | Yes |
Precompiling ColdFusion Templates
As you may know, the ColdFusion server compiles each ColdFusion template into Java bytecode when the template is initially requestedthat is, the first time after a server restart. When the "Save class files" option in ColdFusion Administrator is enabled, the compiler writes the bytecode into Java .class files on the disk and then copies the bytecode into memory (the Template Cache); otherwise, the bytecode is written directly to the Template Cache. This compilation process increases the initial page request. ColdFusion will continue serving the template from the Template Cache, so that only the initial request takes the compilation hit. Combining this technique with the Trusted Cache option can dramatically improve site performance. Macromedia recommends saving the class files only for production servers.Use the following command to compile ColdFusion templates (CFM, CFC, and CFR files) into Java class files:
TIPNotice that the directory-to-compile must be within the specified webroot. If you want to compile templates outside of the actual webroot to class filessuch as templates in a Web server virtual directory called Testingthen specify that directory as the webroot and omit the srcdir parameter: cfcompile C:\Testing.The compiled class files are stored in the cf_root/wwroot/WEB-INF/cfclasses (server configuration) or cf_webapp_root/WEB-INF/cfclasses (J2EE configurations) directory. The files are renamed using a unique syntax:
cfcompile webroot directory-to-compile
ColdFusion uses the following to derive this filename:
cf + filename + hash code + .class
- The filename is extracted from a Java File object created from the canonical filename (for example, C:\Testing\hasher.cfm) with the following substitutions:Included slashes (/) in the filename are replaced with a double underscore (__).Any characters that are illegal in a Java identifier are replaced with their equivalent two-digit hexadecimal number; for example, a period (.) becomes 2e.
- The hash code represents the hash code value created by the Java File object (java.io.File). The hash code is generated by calling the hashCode method; refer to the Java documen tation at http://java.sun.com/j2se/1.4.2/docs/api/java/io/Fil218#hashCode().If the value returned by hashCode() is negative, it is exclusive-or'd with 0xFFFFFFFF to get the value ColdFusion uses.
TIPYou will also see .class files with a dollar sign ($) in the name followed by a function name, as in cfApplication2ecfc639310892$funcONREQUESTSTART.class. The $ represents the compiled ColdFusion user-defined function (UDF) calls within the file. This is more common for ColdFusion Components (CFC) files, but they will occur for any ColdFusion template containing UDF calls.
ColdFusion Template Deployment Considerations
At first glance, the difference in the command syntax for compiling your ColdFusion templates to .class files as compared with compiling to sourceless code is the deploy option. However, the real difference is the process by which the code is compiled. When precompiling (or not using the deploy option), the compiler translates the CFML into Java bytecode and writes the bytecode to a .class file. When creating sourceless templates, the compiler implements the ColdFusion run-time service, translates the CFML into Java bytecode, and then creates a new template (with the same name as the original) and writes the bytecode into that template. Implementing the run-time service allows the compiler to check the code for syntax errorsas if the code were called from the browser. The compiler will fail and return if there are any errors in your code. The error and stack trace are recorded to the ColdFusion exception log (cfroot\logs\exception.log).TIPThe deploy option will catch compilation errors, but some run-time errors will not occur until after deployment. For example, "Template not found" errors for cfinclude will not occur until the deployed file is run on the deployed system.Macromedia provided the cfcompile script with ColdFusion for precompiling .class files. Some developers have tried deploying these files on other systems. Macromedia does not recommend deploying compiled .class files, however, because they are largely dependent on the underlying directory structure of the source server. Some of the class files might actually work on the deployment server. Macromedia created the deploy option to enable the secure deployment of ColdFusion templates.
Customizing the cfcompile Script
You can customize the cfcompile script file that ships with ColdFusion MX 7, or build your own. [View full width]java -cp "c:\cfusionmx7\runtime\lib\jrun.jar;C:\CFusionMX7\wwwroot\WEB-INF\lib





OPTION | DESCRIPTION | REQUIRED |
---|---|---|
cfroot | The fully qualified path to the ColdFusion root directory (such as C:\CFusionMX7 or /opt/coldfusionmx7). | Yes |
d or deploy | Compile templates for sourceless deployment. | Yes for Sourceless Deployment |
deploydir | The fully qualified path to the directory that will contain the compiled deployable files. This cannot be the same directory as the source directory. | Yes for Sourceless Deployment |
f or force | Force compilation. | No |
g or debug | Enable debug symbols. | No |
help | Displays usage information and available options. | No |
srcdir | The fully qualified path to the directory containing the templates to be compiled. This directory must be under the specified webroot directory. If omitted, ColdFusion will compile all ColdFusion templates in the specified webroot directory. | Yes |
webinf | The fully qualified path to the ColdFusion WEB-INF directory. | Yes |
webroot | The fully qualified path to the Web server root directory (such as C:\CFusionMX7\wwwroot or C:\Inetpub\wwwroot). | Yes |
v or verbose | Display compiler performance statistics. | No |
If you try using a wildcard (as in C:\InetPub\wwwroot\*.cfx), only the first file is compiled. See the "Filename Extension" section in Chapter 10 for details on adding additional ColdFusion filename extensions.
cfcompile deploy C:\InetPub\wwwroot C:\InetPub\wwwroot\index.cfm C:\Testing\source