Using .jar (Java Archive) file with ColdFusion
"Java is running under the hood of ColdFusion and it is easier to use Java / Java Archives" with ColdFusion. I have been hearing about this since I have started studying ColdFusion. So I decided to take a look at it. I have studied Java only during my course and does not have much experience with it. Now, with the help of Eclipse, it is very easy to create a Java project and export it as archive.
The first "Search" application. I started with a new Java Project in Eclipse. Created the first search.java file with the package name “org.prasanthkumars.search”. I wrote a new "public function" that simply returns the passed key appended with a random number and called it from the main function. I did this for testing the code.
I ran the code and, you know (no, no error this time :-) ), got the result displayed to console.
That is it. Now, I need to package this file into an archive. Of course, Eclipse will do that for me.
Just check whether the .jar file is working correctly.
So, our .jar file is working fine. Now go ahead and register this .jar file in ColdFusion server.
Now you can access this jar file from ColdFusion code.
This will output the string “hello world” and a random number.
The first "Search" application. I started with a new Java Project in Eclipse. Created the first search.java file with the package name “org.prasanthkumars.search”. I wrote a new "public function" that simply returns the passed key appended with a random number and called it from the main function. I did this for testing the code.
package org.prasanthkumars.search;
import java.util.Random;
public class Search {
public static void main(String[] args) {
System.out.println(searchResult("hello"));
}
public static String searchResult(String key){
Random randomGenerator = new Random();
return key+" "+randomGenerator.nextInt(500);
}
}
import java.util.Random;
public class Search {
public static void main(String[] args) {
System.out.println(searchResult("hello"));
}
public static String searchResult(String key){
Random randomGenerator = new Random();
return key+" "+randomGenerator.nextInt(500);
}
}
I ran the code and, you know (no, no error this time :-) ), got the result displayed to console.
That is it. Now, I need to package this file into an archive. Of course, Eclipse will do that for me.
- Go to File -> Export -> JAR file.
- In the next Pop-up, select the Search.java file and also give the destination for the JAR file.
- Select Next and select the required options.
- Select Next and select the Main Class (in our case it is ‘Search’).
- Click Finish and you have the org.prasanthkumars.search.jar saved to your system.
Just check whether the .jar file is working correctly.
- In the windows Command Prompt, go to the folder in which the .jar file is exported.
- Type java -jar org.prasanthkumars.search.jar and click enter.
So, our .jar file is working fine. Now go ahead and register this .jar file in ColdFusion server.
- Go to CF Administrator -> Server Settings > Java and JVM.
- Enter the path of the jar file under “ColdFusion Class Path” (eg: "C:\JavaProjects\search\org.prasanthkumars.search.jar". Add additional paths separated by Commas).
- Click “Submit Changes”.
Now you can access this jar file from ColdFusion code.
<cfset javaobj="CreateObject("java","org.prasanthkumars.search.Search")" />
<cfoutput>javaObj.searchResult("hello world")</cfoutput>
<cfoutput>javaObj.searchResult("hello world")</cfoutput>
This will output the string “hello world” and a random number.
Comments
Thanks