Invoking Java methods in JavaScript
You can invoke Java methods from the workstep using JavaScript. You might want to do this to perform complex calculations, perform sophisticated business analysis, or to leverage certain functionality within an existing tool. To invoke a static Java method from JavaScript, use the following syntax:
Packages.<fully_qualified_java_class_name>.<static_method_name>
To instantiate a new Java object, use the following syntax:
var jobj = new Packages.<fully_qualified_class_name>(params);
To instantiate a new array of a Java class, use the following syntax:
var dsNames =
java.lang.reflect.Array.newInstance(<fully_qualified_class_name>, <array length>);
For example, to create an instance of a string array of size 3, use the following code:
var dsNames =
java.lang.reflect.Array.newInstance(java.lang.String, 3);
The JavaScript Engine uses the keyword Packages to identify the Java classes and methods.
Note: The keyword Packages is not required for classes in Sun Java's JDK, in classes.zip , or in rt.jar .
The following is an example of how to invoke Java methods in JavaScript:
Java program: Employee.java
package com.tdiinc;
public class Employee {
protected static String companyName;
protected String empName;
Employee(String ename) {
empName = ename;
}
public static String getcompanyName() {
return(companyName);
}
public String getempName() {
return empName;
}
}
JavaScript:
function f1() {
var cmpname=Packages.com.tdiinc.Employee.getcompanyName();
var empObj = new Packages.com.tdiinc.Employee("George")
var ename = empObj.getempName();
}