Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
What if I choose an option not to compile java file for jdk1.6, but to change ColdFusion's java.home to work with jdk1.7. Should this solve the problem?
The reason I am asking is that I've created a module (a jar file with lots Java/Scala libraries packed into it), which uses a lot of external java libraries, and when I am trying to run a certain function from this module, I again encounter the same problem – the output of the page cuts off after the call to that function. I do manage to load the class from this .jar file and everything works fine until I call a certain method of this class (this method uses a lot of libraries internally). After a call to this method, the output of the page is cut off.
At first I thought that probably one of the libraries is built for jdk1.7, and when this library is used in my module, then the problem occurs. Indeed I found among the libraries that there is a Netty-3.4.4.Final library for which it is said in the manifest file that the jdk is 1.7 (though on the projects GutHub page it is said that Netty needs jdk 1.7 to compile but will run on jdk from 1.5).
Then, remembering your words about CF10 update to Java 7, I started to look for possibilities to run CF10 on jdk1.7, and I actually managed to do it (by changing java.home). I hoped this would solve the problem with jdk1.7 java libraries, but unfortunately it did not.
Copy link to clipboard
Copied
As you yourself have discovered, if you configure Coldfusion 10 to work with Java 1.7, it may run the files you compiled with JDK1.7, however it wont run a lot of ColdFusion's own system files! That is because ColdFusion 10's Java libraries are compatible with Java 1.6.
Those are the current Java upgrade rules for ColdFusion. However, be patient. The ColdFusion Team's Hemant Khandelwal says ColdFusion 10 will soon support Java 1.7.