Embedding Java code in TTCN-3 as external function [message #1730737] |
Thu, 28 April 2016 07:32 |
|
Dear all,
In similarity with the previous Python example (https://www.eclipse.org/forums/index.php/t/1068933/), the Titan C/C++ API can be used to embed Java code as well.
Let this be the Java code we want to execute(MainClass.java):
public class MainClass {
public static void print() {
System.out.println("\n Hello Java, Titan is calling!\n");
}
public static void main(String[] args) {
System.out.println("[ Test Titan call to Java using JNI ]");
print();
return;
}
}
Then this could be the "glue code" which uses the Java native interface (MyJava.cc):
#include "MyJava_Functions.hh"
#include <jni.h>
#include <iostream>
namespace MyJava__Functions{
INTEGER f__javaExecute(){
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;
JavaVMOption options[1];
options[0].optionString= (char *) "-Djava.class.path=/home/ethlel/EmbedJava";
vm_args.version= JNI_VERSION_1_6;
vm_args.nOptions=1;
vm_args.options= options;
vm_args.ignoreUnrecognized= JNI_FALSE;
int ret = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
if(ret < 0){
std::cout<< "\nUnable to Launch JVM\n";
return -1;
} else{
helloWorldClass= env->FindClass("MainClass");
if(helloWorldClass == NULL){
goto destroy;
}
mainMethod= env->GetStaticMethodID(helloWorldClass, "main", "([Ljava/lang/String;)V");
if(mainMethod == NULL){
goto destroy;
}
applicationArgs= env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL);
if(applicationArgs == NULL){
goto destroy;
}
applicationArg0= env->NewStringUTF("From-C-Program");
env->SetObjectArrayElement(applicationArgs, 0, applicationArg0 );
env->CallStaticVoidMethod(helloWorldClass, mainMethod, applicationArgs);
destroy:
if(env->ExceptionOccurred()){
env->ExceptionDescribe();
}
jvm->DestroyJavaVM();
return 0;
}
}
}
the function f__javaExecute() declared above can be used as a TTCN-3 external function(MyJava_Functions.ttcn):
module MyJava_Functions
{
external function f_javaExecute() return integer;
}
which in turn can be used from the TTCN-3 code when imported(MyJava_test.ttcn):
module MyJava_test
{
import from MyJava_Functions all;
type component MTC_CT
{
}
testcase TC_0() runs on MTC_CT
{
log("---------------------------");
log(f_javaExecute());
log("---------------------------")
}
control
{
execute(TC_0());
}
}
Let's build and test our Java code:
javac MainClass.java
java MainClass
we should see:
[ Test Titan call to Java using JNI ]
Hello Java, Titan is calling!
Let's generate the Makefile for *.ttcn and *.cc components:
makefilegen -e MyJava_test -s MyJava_test.ttcn MyJava_Functions.ttcn MyJava.cc
and compile:
check if required dynamic libraries can be seen:
If needed, set LD_LIBRARY_PATH, e.g.:
setenv LD_LIBRARY_PATH /home/user/Tools/jdk1.7.0_45_x86_64/jre/lib/amd64/server:${LD_LIBRARY_PATH
set CLASSPATH if needed, e.g. :
setenv CLASSPATH .:${CLASSPATH}
now, you can run MyJava_test:
./MyJava_test
TTCN-3 Test Executor (single mode), version CRL 113 200/5 R5A
Execution of control part in module MyJava_test started.
Test case TC_0 started.
[ Test Titan call to Java using JNI ]
Hello Java, Titan is calling!
Test case TC_0 finished. Verdict: none
Execution of control part in module MyJava_test finished.
Verdict statistics: 1 none (100.00 %), 0 pass (0.00 %), 0 inconc (0.00 %), 0 fail (0.00 %), 0 error (0.00 %).
Test execution summary: 1 test case was executed. Overall verdict: none
I hope this might help anyone interested.
Best regards
Elemer
|
|
|
|
|
|
|
Re: Embedding Java code in TTCN-3 as external function [message #1731138 is a reply to message #1731120] |
Tue, 03 May 2016 07:13 |
|
Hi Alexandre,
I got confused too, as what I said above could not be the reason for this symptom.
OK , try this:
in Makefile,
add your relevant path:
# Flags for the linker:
LDFLAGS = -L/proj/TTCN/Tools/jdk1.7.0_45_x86_64/jre/lib/amd64/server/
Also in LINUX_LIBS you need to add -ljvm:
LINUX_LIBS = -lxml2 -ljvm
I hope this helps
Elemer
|
|
|
|
Powered by
FUDForum. Page generated in 0.05772 seconds