Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Titan » Embedding Java code in TTCN-3 as external function(Java-Eclipse Titan interworking)
Embedding Java code in TTCN-3 as external function [message #1730737] Thu, 28 April 2016 07:32 Go to next message
Elemer Lelik is currently offline Elemer LelikFriend
Messages: 760
Registered: January 2015
Senior Member
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:

make


check if required dynamic libraries can be seen:

ldd MyJava_test


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 #1730903 is a reply to message #1730737] Fri, 29 April 2016 15:45 Go to previous messageGo to next message
Gustavo Gonnet is currently offline Gustavo GonnetFriend
Messages: 34
Registered: October 2015
Location: Montreal, Quebec, Canada
Member
very clear example, thanks a lot
Re: Embedding Java code in TTCN-3 as external function [message #1730995 is a reply to message #1730903] Sun, 01 May 2016 23:08 Go to previous messageGo to next message
Alexandre Moine is currently offline Alexandre MoineFriend
Messages: 9
Registered: April 2016
Junior Member
Hi

I have some issues with the compilation:
MyJava.o: In function `MyJava__Functions::f__javaExecute()':
MyJava.cc:(.text+0x41): undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status
make: *** [MyJava_test] Error 1

In the makefile I added the path to jdk because in the first time jni.h was unrecognized.
After all, it seems to be the same problem, so.. Any suggestions?
Thank you for your help by the way
Re: Embedding Java code in TTCN-3 as external function [message #1731011 is a reply to message #1730995] Mon, 02 May 2016 07:06 Go to previous messageGo to next message
Elemer Lelik is currently offline Elemer LelikFriend
Messages: 760
Registered: January 2015
Senior Member
Hi Alexandre,

in My Java.cc

the line:

options[0].optionString= (char *) "-Djava.class.path=/home/ethlel/EmbedJava";

should be changed to reflect your environment.

This could be the reason; pls let me know if it helps.

BR Elemer



Re: Embedding Java code in TTCN-3 as external function [message #1731120 is a reply to message #1731011] Tue, 03 May 2016 00:49 Go to previous messageGo to next message
Alexandre Moine is currently offline Alexandre MoineFriend
Messages: 9
Registered: April 2016
Junior Member
Hi,

Same error even with the modification. By the way my bad for not having see this part of code, really a shame.
I must admit I'm lost because I really don't understand where is the error. Maybe I don't use java includes in right way.

CPPFLAGS = -D$(PLATFORM) -I$(TTCN3_DIR)/include \
                                                    -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ \
				                    -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/linux/ \
				                    -L/usr/bin/java \
				                    -L/usr/lib/jvm/java-1.7.0-openjdk-amd64/jre/lib/amd64/server/ \


First time so, I guess I made mistakes.
Re: Embedding Java code in TTCN-3 as external function [message #1731138 is a reply to message #1731120] Tue, 03 May 2016 07:13 Go to previous messageGo to next message
Elemer Lelik is currently offline Elemer LelikFriend
Messages: 760
Registered: January 2015
Senior Member
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
Re: Embedding Java code in TTCN-3 as external function [message #1731256 is a reply to message #1731138] Tue, 03 May 2016 20:27 Go to previous message
Alexandre Moine is currently offline Alexandre MoineFriend
Messages: 9
Registered: April 2016
Junior Member
Hi Elemer

It works! Thanks a lot, well done!
Previous Topic:The issue of Eclipse Titan on Windows revisited
Next Topic:Generating Java code from TTCN-3
Goto Forum:
  


Current Time: Fri Jun 22 07:35:27 GMT 2018

Powered by FUDForum. Page generated in 0.01578 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top