You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

Compilation and Precompilation in CFMX Templates Part 1

Compilation and Precompilation in CFMX Templates Part 1

Most of you have heard by now that ColdFusion MX templates are compiled into Java. Some may wonder what the big deal is.

Many more of you may be surprised to learn that you can precompile CFMX templates. Do you know why you might want to? And if you've known previously about how to precompile CFMX templates, did you find that trying to do so with code outside the default\coldfusionmx\wwwroot directory (or \inetpub\wwwroot for IIS users) was troublesome?

I've got the solution to that problem as well. First, some background to bring everyone up to speed on the subject of compilation and precompilation in CFMX.

CFMX Templates Are Compiled
One of the great new benefits of ColdFusion MX is that, because it's built on an underlying Java platform, our ColdFusion templates are actually compiled into Java classes. They're not really executables, though, and they're not self-sufficient programs that can run on any Java (or even J2EE) platform, but the compilation takes place nonetheless.

Unlike in Java and many other languages, however, this compilation takes place automatically. Just as with previous releases of ColdFusion, you can create or edit your templates, and ColdFusion simply runs them as soon as they're changed. (Actually, JSP templates in the J2EE world work the same way.)

You needn't really concern yourself with the mechanisms of automatic compilation. It works without your doing anything. So why this article?

The Onetime Cost of Compilation to a User
Each of these features, the automatic compilation and the automatic detection of code changes, is a two-edged sword. There's the cost borne by the first user to execute a page after it's been created or edited. The request for that Web page will be delayed for a few seconds while CFMX compiles the code the first time. Some find this cause for concern.

The benefit of compiling, of course, is that once this "cost" has been paid by the first user to hit the page, each subsequent page hit is very fast; this is a onetime cost and recurs only if the file is later changed. That compiled code is saved and stored on disk (as a .class file that CF knows how to find and execute), and even if the server is restarted, that previously compiled file will be reused by the server to serve that template. There won't be any more waits for compilation until a developer changes the template again. (Well, almost. More about that in Part 2.)

But what if you deploy a large number of newly created or edited templates in your application? Or what if you frequently change them throughout the day? Then the number of automatic compilations grows. Consider the impact on the first users who hit such changed pages, or the first user hitting your multipage site after you've deployed a number of new templates. That first user may suffer delays on every page as he or she traverses through your site. Ouch.

Anyone who has gone through the ColdFusion Administrator for the first time after installing CFMX will understand the experience. At least that code doesn't change again after CFMX is installed. But you may hit a page some days later that no one else has hit yet, and you'll wait for that compilation then.

First-Time Wait Not Entirely New
Actually, the penalty borne by the first user to run a template after it's created or edited isn't exclusive to CFMX. In CF5 and before, CF also "interpreted" any new or changed CF template, saving the resulting p-code to memory in CF's template cache. But since the process was writing to memory rather than disk, it wasn't quite as noticeable.

Then again, because it wasn't saved to disk, this first-time interpretation had to take place whenever the ColdFusion server was restarted. (The template cache will be discussed further in Part 2.) This need to reinterpret templates on restart led many to complain about the need for real compilation of code to disk.

So compilation is good. But is there anything you can do to avoid the wait by the first executing user? Is this just the price to be paid for the power of automatic compilation?

Precompilation: Avoiding First-User Cost
Part of the answer lies in the question itself. We're relying on the automatic compilation that occurs when the first user runs the template after it's created or edited. What if you could somehow tell ColdFusion to perform that compilation manually? Wouldn't it be useful if there were a command you could issue against your template directory to say "compile all those templates"?

Macromedia didn't provide us with that, and there may be very good reasons (put the conspiracy theories on hold - we're talking about the same compilation that takes place automatically, so protecting intellectual property is not what kept them from offering it).

But a simple three-line batch file has been making the rounds of various mailing lists and blogs (mine included, www.cfmxplus.blogspot.com). The initial version of this batch file seemed to give us the apparent silver bullet we've sought for slaying the first-time delay. The problem was, it only worked for code that was in the default webroot if you followed its original instructions.

I present here a version that does work for CFMX code in any directory. I've also reduced it to just two lines. Simply create a file called precompile.bat with the following lines:

set MX_INSTALL=c:\cfusionmx
%MX_INSTALL%\lib\cfusion.jar coldfusion.tools.Compiler
-webroot %1 -webinf %MX_INSTALL%\wwwroot\WEB-INF %1

Just to be clear, there should be two lines in the file: one starting with "set" and one starting with "%MX_INSTALL%".

Replace the first line's c:\cfusionmx value with the location of the CFMX installation. Again, don't worry if your code isn't stored under the wwwroot in that directory. I'll show in a moment that the batch file will accept a parameter that tells it where your code is actually stored, so you can use this over and over to precompile code in different directories.

Save this file as precompile.bat anywhere on your system. Then, from the DOS command prompt, run it from whatever directory you saved the batch file in. If you're not familiar with getting to the DOS command prompt or running .bat files from a specific directory, please seek help from someone with that knowledge.

Now you can execute it, naming the directory whose files (and subdirectories of files) you want to compile, as in:

precompile c:\cfusionmx\wwwroot\mydir


precompile d:\inetpub\wwwroot\somedir


precompile d:\myotherdir

That third example demonstrates the compilation of code that isn't stored in the normal webroot. To learn how to be able to access such a directory using the built-in CFMX Web server, see the entry at my blog site, http://cfmxplus.blogspot.com/2002_08_11_ cfmxplus_archive.html#85347963.

The point is, this version of the batch file will compile any directory of CF templates as long as you specify the complete path to the file (see the next section for other differences between this batch file and the one that was passed around the community a couple of months ago).

Again, note that I said it will compile the files in the named directory and any subdirectories. That's useful if you're expecting it, but annoying if you aren't. I haven't figured out how to keep it from recursing through the subdirectories. If anyone has figured that out, please let me know by e-mail or via the online version of this article at the www.sys-con.com/coldfusion Web site.

If the command is successful, you'll see a display (perhaps a lengthy one) indicating that it's compiling each file, one at a time, and reporting how long it takes. It also reports any syntax errors in the code. Very nifty!

It even repeats the list of errors at the end of the display for easy review. Depending on your operating system, you may be able to scroll up and down in the command window to review the results that have scrolled off the page. Of course, the compiler is smart enough not to try to compile anything other than .cfm and .cfc files.

Note that you could use this to precompile all the files in your entire wwwroot (including subdirectories), using the command:

precompile c:\cfusionmx\wwwroot\

as an example, assuming that's where the default webroot is, or on IIS:

precompile c:\inetpub\wwwroot\

Just be aware that this will also ask it to compile such things as the CFDOCS and CFIDE directories that come installed with CFMX. That's over 2,000 files, not counting your own! Use the tool wisely.

And it may be useful to some to be able to compile just a single file, rather than an entire directory and its subdirectories. You can do that, too, as in:

precompile c:\cfusionmx\wwwroot\myfile.cfm

Finally, I haven't been able to get it to accept a pattern, at least not in this simple two-line version of the batch file.

As an aside, you may wonder if the compiler called by the precompile batch file knows to skip files it's already compiled. It doesn't skip them, but you'll notice that as it reports on each template it finds, the compilation time reported will usually be zero seconds for a file that hasn't changed since the last compile.

Indeed, I've learned from Spike Washburn's blog (thanks to a pointer from Dave Watts) that there's an available "-f" directive to force a compilation of the templates even if they've already been compiled once. One possible use for this is when something seems stuck in the way CF is interpreting a template and you want to force a recompile.

You could modify the script to add a -f argument before the -webroot argument. You could also enable it as a parameter to be passed with a few extra lines of batch file code.

Other Versions of precompile.bat
As I mentioned, the batch file I offer above is slightly different from one that was passed around among beta testers and early users of CFMX a couple of months ago. That version used a value for the -webroot directive on the compiler command line (the "java" line) that caused it not to work for compiling code outside the default webroot.

Some learned that they could change that value to point to their desired directory, but then they learned that in order for it to work, they had to go through some more hoops. What was lacking was two things. First, the critical -webinf compiler directive, a hidden gem I learned from Harry Klein of CONTENS Software in Germany.

As my batch file code above shows, it names the path to the wwwroot\WEB-INF directory where CFMX is installed. Without this, if you pointed the -webroot directive at some location other than the default wwwroot, the compiler would fail because it expected to find that WEB-INF directory and several subdirectories of it under the directory you named in the -webroot directive.

Furthermore, I've set the -webroot directive to be whatever directory you pass on the call to the precompile.bat, rather than pointing it at the default webroot. These two things were the missing pieces that kept the previous versions of this precompile.bat file from working against any directory.

I've also changed it so that you must provide the complete path to the directory containing the code you want to compile (whereas the older one worked with relative paths depending on where you placed the precompile.bat file). This makes mine more robust for a few reasons, at the cost of a few extra keystrokes when passing directory names to the precompile batch file.

Readers familiar with batch file variables will notice that I've also set it up to accept only a single argument naming the directory path or file holding the code to be compiled. The older version allowed specification of multiple directories or files. Again, it worked fine with code in the default webroot, but in order to support code in any directory in just two lines, that feature had to be eliminated.

Indeed, you could change it to accept multiple directories, but with the way the batch file is currently written, they'd have to be subdirectories of the first one named, and that's not useful since it already compiles all the subdirectories of any provided. You couldn't name a parent and only one of its children, for instance, since the naming of the parent would already compile all the children.

One other minor change in this version and others offered previously is that I'm prefixing the call to the java interpreter (in the second line of the batch file) with the location within the CFMX install directory where the default Java Virtual Machine is installed. This might prevent some confusion if multiple JVMs are installed.

With more tweaking and some additional DOS commands and conditional tests, the batch file code could be made to provide additional functionality and address some of these issues. I think most will be glad simply to be able to precompile code at all, in more than just the default webroot, and in just two simple lines of code.

Finally, for those working on Solaris and Linux, ColdFusion developer Matt Liotta has offered a version of that batch file as a shell script:


$MX_INSTALL/jre/bin/java -class
lib/cfusion.jar \coldfusion.
tools.Compiler -webroot $1\
-webinf $MX_INSTALL/wwwroot/

Just to be clear, there should be three lines of code in the file, one starting with "MX_INSTALL", one starting with "PATH", and one starting with "$MX_INSTALL/jre/bin/java".

Again, be sure to change line one to the location where CFMX is installed. Thanks, Matt!

*  *  *

We could stop at this point - and will. The problem of precompiling code is solved. But there will be curious folks (and bit-twiddlers) among you who will want to know more, maybe lots more.

How much time is this really saving? If it's compiled to disk, how and when does CFMX read it into memory to execute it? What's the cost of that? What happens with CF-INCLUDEd files? Where does the compiled code go? Can I look at it? Can I just delete the generated class files instead? How do I determine which class file was generated for which CF template? Can I distribute the compiled code on other servers without the source code? (The answer to the last question, sadly, is no.)

We'll discuss that and the rest of these questions next month. Otherwise, you've got all you need to know to start precompiling code in CFMX.

More Stories By Charlie Arehart

A veteran ColdFusion developer since 1997, Charlie Arehart is a long-time contributor to the community and a recognized Adobe Community Expert. He's a certified Advanced CF Developer and Instructor for CF 4/5/6/7 and served as tech editor of CFDJ until 2003. Now an independent contractor (carehart.org) living in Alpharetta, GA, Charlie provides high-level troubleshooting/tuning assistance and training/mentoring for CF teams. He helps run the Online ColdFusion Meetup (coldfusionmeetup.com, an online CF user group), is a contributor to the CF8 WACK books by Ben Forta, and is frequently invited to speak at developer conferences and user groups worldwide.

Comments (9)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
The deluge of IoT sensor data collected from connected devices and the powerful AI required to make that data actionable are giving rise to a hybrid ecosystem in which cloud, on-prem and edge processes become interweaved. Attendees will learn how emerging composable infrastructure solutions deliver the adaptive architecture needed to manage this new data reality. Machine learning algorithms can better anticipate data storms and automate resources to support surges, including fully scalable GPU-c...
Machine learning has taken residence at our cities' cores and now we can finally have "smart cities." Cities are a collection of buildings made to provide the structure and safety necessary for people to function, create and survive. Buildings are a pool of ever-changing performance data from large automated systems such as heating and cooling to the people that live and work within them. Through machine learning, buildings can optimize performance, reduce costs, and improve occupant comfort by ...
The explosion of new web/cloud/IoT-based applications and the data they generate are transforming our world right before our eyes. In this rush to adopt these new technologies, organizations are often ignoring fundamental questions concerning who owns the data and failing to ask for permission to conduct invasive surveillance of their customers. Organizations that are not transparent about how their systems gather data telemetry without offering shared data ownership risk product rejection, regu...
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Poor data quality and analytics drive down business value. In fact, Gartner estimated that the average financial impact of poor data quality on organizations is $9.7 million per year. But bad data is much more than a cost center. By eroding trust in information, analytics and the business decisions based on these, it is a serious impediment to digital transformation.
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Predicting the future has never been more challenging - not because of the lack of data but because of the flood of ungoverned and risk laden information. Microsoft states that 2.5 exabytes of data are created every day. Expectations and reliance on data are being pushed to the limits, as demands around hybrid options continue to grow.
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Enterprises have taken advantage of IoT to achieve important revenue and cost advantages. What is less apparent is how incumbent enterprises operating at scale have, following success with IoT, built analytic, operations management and software development capabilities - ranging from autonomous vehicles to manageable robotics installations. They have embraced these capabilities as if they were Silicon Valley startups.
As IoT continues to increase momentum, so does the associated risk. Secure Device Lifecycle Management (DLM) is ranked as one of the most important technology areas of IoT. Driving this trend is the realization that secure support for IoT devices provides companies the ability to deliver high-quality, reliable, secure offerings faster, create new revenue streams, and reduce support costs, all while building a competitive advantage in their markets. In this session, we will use customer use cases...