Skip to main content
Known Participant
December 15, 2012
Question

"Creating and using a simple Java class" tutorial from docs doesn't work

  • December 15, 2012
  • 2 replies
  • 1428 views

Hi everybody.

I am trying to follow this tutorial on creating a simple Java class and using it in a simple ColdFusion 10 app, but with no luck.

I believe I am doing everything as the tutorial says, I create an Employee.java file and compile it with javac (I think mine one is from jdk1.7.0_02). I add javaSettings to Application.cfc like the following:

Application.cfc

...

this.javaSettings.loadPaths = [ExpandPath('./jars')];

this.javaSettings.loadColdFusionClassPath = false;

this.javaSettings.reloadOnChange = true;

...

Index.cfm

<p>Starting..</p>

<cfobject action="create" type="java" class="Employee" name="emp">

<p>Are we still running?</p>

<!--- <cfset emp.init()> --->

<cfset emp.firstname = "john">

<cfset emp.lastname = "doe">

<cfset firstname = emp.firstname>

<cfset lastname = emp.lastname>

<cfoutput>

    Employee name is #firstname# #lastname#

</cfoutput>

<p>Finishing..</p>

Folder strcuture

Application.cfc

index.cfm

jars/

Employee.class

When I try to run this code, I get only "Starting..". Laterally. Everything after cfobject tag just seems not to be evaluated at all. Zero. Even static content like "Are we still running?" and "Finishing.." is not being output to the page.

If I try to cfdump "emp", I still get nothing (nothing is output to the page.. like absolute zero, no contents at all).

And it doesn't show any error. For the sake of testing I even tried to deliberately misspell class name in "class" attribute to make sure exception is thrown, and it is thrown if class name is incorrect. But not with the code shown above.

I am running ACF10 (with update 6) on Windows 7.

How can I get this simple example to work?

Thank you.

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
December 15, 2012

I create an Employee.java file and compile it with javac (I think mine one is from jdk1.7.0_02).

That probably caused the problem. ColdFusion 10 is based on Java 1.6.x. So compile the file with JDK1.6.x and restart the entire process.

Known Participant
December 15, 2012

Thanks for the response! Indeed, classes compiled with the following flags are loaded into ColdFusion and work properly:

>javac -source 1.6 -target 1.6 Employee.java

I know some people have mentioned on the internet that they have working Scala code in ColdFusion but unfortunately they reveal no details.

Known Participant
December 15, 2012

I've just tried to compile the same file with a JavaCompiler.cfc bundled in the latest version of Mark's JavaLoader.

And, guess what, the compiled classes do get loaded properly.

So is there a problem with javac that I use? Is it too new for ColdFusion 10? Or are there any special setting I have to consider when compile java files?

Actually there is a reason, why using the JavaCompiler.cfc will not work for me. I actually want to try to use Scala, and that's why I need to be able to use external compiler. By the way, if anybody here managed to load Scala classes into ColdFusion, please please please do share you experience.