Summary
A LegacyPlug sample which demonstrate calling Java Class Functions from C (DLL) Using JNI The LegacyPlug will pass an integer value to Java Class, the java program will do calculation and return square of the integer back to C, and finally pass back to AIS adapter. This sample running well on AIS 5.3.2.50 on Windows.

Detailed steps and sample programs as below:

1. A java program, complie by "javac Sample2.java"

public class Sample2
{
public static int intMethod(int n) {
return n*n;
}

public static boolean booleanMethod(boolean bool) {
return !bool;
}
}



2. A C program, compiled to a DLL by VS2005 on Windows platform

#include <jni.h>
#include <string.h>

#ifdef WIN32
# define EXPORT_SYMBOL __declspec(dllexport)
#else
# define EXPORT_SYMBOL
#endif
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif

EXPORT_SYMBOL int CcallJava(int input,int *result)
{
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
jboolean not;

options[0].optionString = "-Djava.class.path=G:\\CcallJava\\javaClass";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

if (status != JNI_ERR)
{
cls = (*env)->FindClass(env, "Sample2");
if(cls !=0)
{ mid = (*env)->GetStaticMethodID(env, cls, "intMethod", "(I)I");
if(mid !=0)
{ square = (*env)->CallStaticIntMethod(env, cls, mid, input);
printf("Result of intMethod: %d\n", square);
}

mid = (*env)->GetStaticMethodID(env, cls, "booleanMethod", "(Z)Z");
if(mid !=0)
{ not = (*env)->CallStaticBooleanMethod(env, cls, mid, 1);
printf("Result of booleanMethod: %d\n", not);
}
}

(*jvm)->DestroyJavaVM(jvm);
return square;
}
else
return -1;
}



3. A LegacyPlug adapter

Adatper definition
<adapter definition="CcallJava" name="CcallJava" type="LegacyPlug">
<config dllName="G:\CcallJava\cdll\Debug\CcallJava.dll"/>
</adapter>

Adapter metadata
<adapterDef name="CcallJava" type="LegacyPlug">
<interaction input="CcallJava_IN" mode="sync-send-receive" name="CcallJava" output="CcallJava_OUT">
<interactionSpec symbolName="CcallJava"/>
</interaction>
<schema>
<record name="CcallJava_IN">
<field mechanism="byValue" name="input" paramNum="1" type="int"/>
</record>
<record name="CcallJava_OUT">
<field mechanism="byValue" name="return" paramNum="0" type="int"/>
</record>
</schema>
</adapterDef>



4. Test by XML Utility