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 #

Post a Comment:
  • HTML Syntax: Allowed

Calendar

« October 2008
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
31
 
       
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.