Compile Java at Runtime with Coldfusion

02:59AM Mar 02, 2008 in category Coldfusion by Jesse Bethke

We have all heard the rumors of dirty little exploits that allows coldfusion to interact with Java objects.  I've finally taken the time to prepare an example of how to write, compile, and use a java class at coldfusion run time.  Imagine a website that programs for us, or programs itself.

Anyways, on Monday, March 4, 2008 at 4pm in Arts & Sciences 216B I'll be offering an express course to Coldfusion development.  The grand finale will be how to utilize java from within coldfusion. I'm going to also note that you will be hard pressed to find another example like this on the net; I know, I looked.

For anyone that knows a lick of Java and Coldfusion programming, the below code will make perfect sense. For everyone else I'll explain what is going on.

The Code

<cfsavecontent variable="javaCode">
	public class CustomUser {
    	private String name;
        
        public void setName(String name) {
        	this.name = name;
        }
        public String getName() {
        	return this.name;
        }
        
    	public String sayHelloTo(String name) {
        	return "Hello "+ name +" my name is "+ this.name;
        }
    }
</cfsavecontent>

<cfscript>
	// Write Code to File
	objFileWriter = CreateObject("java","java.io.FileWriter"); 
	fileLocation = javaCast("String", "#Server.ColdFusion.Rootdir#/lib/CustomUser.java");
	fileWriter = objFileWriter.init(fileLocation, false); 
	fileWriter.write(javaCast("String", javaCode));
	fileWriter.flush();
	fileWriter.close();
	
	// Compile Class
	byteArray = CreateObject("java","java.io.ByteArrayOutputStream").init(); 
	compileParams = javaCast("String", "-classpath,placeHolder,#fileLocation#"); 
	javaCompiler = CreateObject("java","sun.tools.javac.Main").init(byteArray, compileParams);
	javaCompiler.compile(compileParams.split(","));
	
	// Use the Class
	johnUser = CreateObject("java", "CustomUser").init();
	johnUser.setName(javaCast("String", "John"));
	
	janeUser = CreateObject("java", "CustomUser").init();
	janeUser.setName(javaCast("String", "Jane"));

	writeoutput(janeUser.sayHelloTo(johnUser.name));
</cfscript>

See it working here:
http://bethke.ws/learnColdfusion/java.cfm (When my server happens to be up)

How it Works 

The general idea is that we write a Java class into a string, write that string to file, compile the file, and then instantiate the class in coldfusion.

Coldfusion's cfsavecontent saves a stream (the contents of its tags) to a string.  While we could write this to file with cffile, it's more l33t to use the java fileWriter.  Lines 22-25 do just that. Note that we are writing it to the lib directory of the coldfusion root.  While we could write it anywhere, it needs to be in a mapped java class path for coldfusion to be able to find it later.

We then use Sun's javac compiler to compile the class file we wrote.  Fortunately, it really is as easy as passing it the filepath.  However, it isn't going to warn you if there is a compile error. It just won't write the .class file.

The we simply instantiate and use the class like any other java class in coldfusion. Pretty sweet huh? 

Why?

I'm actually not 100% sure what the benefits of this would be.  However, this feature could be used to dynamically render java code based on user input, data, or environment variables.  More importantly, Java offers many features that coldfusion does not.  Being able to use Java classes adds a new level of power to Coldfusion development.  Fact of the matter is though, utilizing many java components in coldfusion is cumbersome. This technique could be used to render and compile more complex Java classes, and then turn around and immediately utilize them.

So why not just use J2EE? Because Coldfusion development is faster and cheaper. Cheaper because it requires less man-hours; even if the server costs more, it is a fixed cost.  It also provides many unique features like on the fly rendering of charts, pdfs, and presentations.

More Dirty Rumors

Supposedly Coldfusion 8 can utilize .Net classes on windows servers. So ASP.Net (VB.net, C#, etc) assemblies could be invoked by Coldfusion. I'd love to see an example of this done with .Net.

Comments:

can i ask you something.If i write .Net then can I do like this ?

Posted by anacondong on May 11, 2008 at 09:49 PM CDT #

so cool !! thank you for content

Posted by Pfeferde on May 11, 2008 at 09:53 PM CDT #

good article, i will test this right now.. thx

Posted by free sms on August 20, 2008 at 10:22 AM CDT #

This is very nice to know. I will ask my webmaster Wible if he can use it as well.

Thank you.

Posted by Wandschablone on October 22, 2008 at 01:55 PM CDT #

Should look for example in .Net. That would also be interesting.

Posted by Suchmaschinenoptimierung on October 23, 2008 at 07:54 AM CDT #

sehr interessanter Artikel, sehr hilfreich und informativ gestaltet. ich werde gleich mal einen versuch starten.

Posted by Free SMS Liste on October 23, 2008 at 04:11 PM CDT #

Thx, its very helpeful for me.

Posted by handy zugabe on October 27, 2008 at 08:31 PM CDT #

It worx!!! This guide really helped me a lot. Thanks for publishing this information.

Posted by Seo on November 21, 2008 at 12:11 PM CST #

Helped me a lot! keep up good work :)

Posted by Videoüberwachung on December 15, 2008 at 03:28 AM CST #

This is very nice to know.

Posted by Werbeagentur on December 15, 2008 at 07:31 AM CST #

Do you have an example?

Posted by Immobilienmakler auf Mallorca on January 22, 2009 at 10:49 AM CST #

Not running with Ubuntu. A pity!

Posted by MPU on January 26, 2009 at 06:14 AM CST #

Ubuntu is only running with linux so far I have the right informations.

Posted by Lavazza on February 07, 2009 at 03:42 PM CST #

Hello Jesse, ould be nice if you write more about Ubuntu. I think many webmasters are interested. Thx

Posted by Notebook kaufen on February 23, 2009 at 05:32 AM CST #

WEll I guess that the topic interests almost everybody, so please report more about it here! Thanks

Posted by Haustüren Coesfeld on February 24, 2009 at 01:24 PM CST #

Doesn´t work: http://bethke.ws/learnColdfusion/java.cfm

Posted by Kork on February 25, 2009 at 02:44 AM CST #

I am not sure if you are totally right in your post Jesse. I tried to follow your tipps, but I wasn´t successfull!

Posted by Lastminute Reisen on February 27, 2009 at 04:30 AM CST #

It´s a very simple tipp. thanks!

Posted by Werbemittel on March 15, 2009 at 05:41 PM CDT #

Post a Comment:
  • HTML Syntax: Allowed

Calendar

« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by The University of South Dakota.