Copy link to clipboard
Copied
Hi,
I am looking to send the array of struct values to the Java method. Whats the better way to do it?
For a simple paratmeter i am able to call method. but i am looking to send the array of struct values to java mthod. but its not working as there is such thing in java.
does any have any solution for this.
This is what i am looking:
<cfset myBook = ArrayNew(2)>
<cfset myBook1 = StructNew()>
<cfset a = StructInsert(myBook1, "title", "Title", 1)>
<cfset a = StructInsert(myBook1, "author", "Author", 1)>
<cfset a = StructInsert(myBook1, "description", "Book Description", 1)>
<cfset a = StructInsert(myBook1, "publishyear", "Publish Year", 1)>
<cfset a = StructInsert(myBook1, "isbn", "ISBN Number", 1)>
<cfset myBook2 = StructNew()>
<cfset a = StructInsert(myBook2, "title", "More ColdFusion", 1)>
<cfset a = StructInsert(myBook2, "author", "David Medlock", 1)>
<cfset a = StructInsert(myBook2, "description", "More ColdFusion Info", 1)>
<cfset a = StructInsert(myBook2, "publishyear", "2005", 1)>
<cfset a = StructInsert(myBook2, "isbn", "321654DCBA", 1)>
<cfset myBookArray = ArrayNew(1)>
<cfset myBookArray[1] = myBook1>
<cfset myBookArray[2] = myBook2>
<cfdump var="#myBookArray#">
<cfobject action = "create" type = "java" class = "SimpleMethod" name = "objSimpleMath">
<cfset retval = objSimpleMath.test(myBookArray )>
I cant able to pass the myBookArray to the java method test. what kind parameter should it be. ?
Thanks in advance.
Srinivas
Copy link to clipboard
Copied
I cant able to pass the myBookArray to the java method test. what kind parameter should it be. ?
That is determined by your java class. We would have to see the java code and error message to advise you on which types to use.
Some objects can be automatically converted, while others you may need to create explicitly with createObject("java", ...). Here is a matrix that describes the conversion of ColdFusion objects to java data types.
http://livedocs.adobe.com/coldfusion/8/htmldocs/Java_8.html
Copy link to clipboard
Copied
But I am using the CF ver 5.0 and Java 1.3. so i cant have all the adv
features.
Srinivas
Copy link to clipboard
Copied
I am not familiar with CF5, but you should check the documentation about what automatic conversions are possible. I cannot be more specific without seeing the java code for your class.
http://www.adobe.com/livedocs/coldfusion/5.0/Developing_ColdFusion_Applications/cfobject8.htm
Copy link to clipboard
Copied
I have written the code in java yet. I am still waiting to know how to send
the parameters from CF to java. Once the java recevies the info then i can
write the require code.
Srinivas
Copy link to clipboard
Copied
vkunirs wrote:
I have written the code in java yet.
You mean you have not written the java code yet. As I mentioned, I am not familiar with CF5. So I can only tell you how MX6+ would behave. Versions MX6+ run on top of java. That means CF structures and arrays already _are_ java objects. So you can typically pass them straight into a java class:
CF psuedo code
<cfset myStruct = structNew()>
<cfset myStruct.foo = "bar">
<cfset myJavaObj = createObject("java", "MyJavaClass")>
<cfset myJavaObj.test( myStruct)>
Java
public void test (java.util.Map dataFromCF) {
...
}
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.html
But since CF5 does not run on top of java it may not work the same way. You may need to use cfobject to create java objects instead. For example a Hashtable instead of a CF structure, and a Vector instead of a CF array. But as I do not use CF5, you will have to try it out yourself.
CF psuedo code
<cfset myStruct = createObject("java", "java.util.Hashtable").init()>
<cfset myStruct.put("foo", "bar")>
<cfset myJavaObj = createObject("java", "MyJavaClass")>
<cfset myJavaObj.test( myStruct)>
Java
public void test (java.util.Map dataFromCF) {
...
}
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html
Copy link to clipboard
Copied
thanks for suggestion. I think i have tried this option. I think these
things will work in 1.4 not in 1.3.
Srinivas
Copy link to clipboard
Copied
Both Vectors and Hashtables exist in java 1.3. So I would think creating an explicit java object would always work. Try it with a simple test class and let us know what happens
import java.util.Hashtable;
import java.util.Map;
import java.util.List;
import java.util.Vector;
public class TestClass {
public static String testMap (Map dataFromCF) {
int count = 0;
if (dataFromCF != null) {
count = dataFromCF.size();
}
return "Map size = ("+ count +")";
}
public static String testList (List dataFromCF) {
int count = 0;
if (dataFromCF != null) {
count = dataFromCF.size();
}
return "List size = ("+ count +")";
}
public static void main(String[] args) {
// verify functions work from java
Map mp = new Hashtable();
mp.put("test", "from java");
String result1 = TestClass.testMap(mp);
System.out.println(result1);
List lst = new Vector();
lst.add("test from java");
String result2 = TestClass.testList(lst);
System.out.println(result2);
}
}
Copy link to clipboard
Copied
Hi
I have writtend the following code:
<cfobject action = "create" type = "java" class = "SimpleTest" name = "objST"> <cfscript> employee = StructNew(); StructInsert(employee, "firstname", "Sri"); StructInsert(employee, "lastname", "vish"); StructInsert(employee, "email", "srinu@gmail.com"); StructInsert(employee, "phone", "11911"); StructInsert(employee, "department", "GIS"); </cfscript> <cfdump var="#employee#"> <Cfset objST.testMap(employee)>
I getting the below error:
Unknown exception during conversion of a CF type to a Java type
The error occurred while processing an element with a general identifier of
(CFSET), occupying document position (11:4) to (11:34) in the template
Srinivas
Copy link to clipboard
Copied
vkunirs wrote:
I have writtend the following code:
Hi Srinivas,
The forum is not showing your CF. Can you post it again?
Thanks
Copy link to clipboard
Copied
Below is the code which i am using:
<cfobject action = "create" type = "java" class = "SimpleTest" name = "objST">
<cfscript>
employee = StructNew();
StructInsert(employee, "firstname", "Sri");
StructInsert(employee, "lastname", "vish");
StructInsert(employee, "email", "srinu@gmail.com");
StructInsert(employee, "phone", "11911");
StructInsert(employee, "department", "GIS");
</cfscript>
<cfdump var="#employee#">
<Cfset objST.testMap(employee)>
Copy link to clipboard
Copied
Try using the java equivalents instead:
java.util.Hashtable ..instead of a CF structure
java.util.Vector .. instead of a CF array
Something like
....
<cfscript>
employee = createObject("java", "java.utl.Hashtable").init();
employee.put("firstname", "Sri");
employee.put("lastname", "vish");
employee.put("email", "srinu@gmail.com");
employee.put("phone", "11911");
employee.put("department", "GIS");
</cfscript>
...
<Cfset objST.testMap(employee)>