Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Use of Java from a C++ application

The differences as compared to using Java from a C application (see section "Use of Java from a C application") are listed below.

Implementation of the C++ code

Let us assume that the Echo.cpp file is implemented as follows:

#include <jni.h>
int
main(int argc, char *argv[])
{
   JavaVMInitArgs vm_args;
   JavaVMOption options[1];
   JavaVM *jvm;
   JNIEnv *env;
   jint res;
   jclass cls;
   jmethodID mid;
   jobjectArray args;
   int i;
   /*
   ** Prepare VM Options
   */
   options[0].optionString = "-Djava.class.path=.";
   /*
   ** Prepare VM configuration
   */
   vm_args.version = JNI_VERSION_1_4;
   vm_args.nOptions = 2;
   vm_args.options = options;
   vm_args.ignoreUnrecognized = JNI_FALSE;
   /*
   ** Create the Java VM
   */
   res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
   if (res < 0)
   {
     fprintf(stderr,"Can't create Java VM\n");
     exit(1);
   }
   /*

   ** Get class Echo
   */
   cls = env->FindClass("Echo");
   if (cls == NULL)
   {
     fprintf(stderr,"Can't find Echo class\n");
     exit(1);
   }
   /*
   ** Get main method
   */
   mid = env->GetStaticMethodID(cls,"main",
               "([Ljava/lang/String;)V");
   if (mid == 0)
   {
     fprintf(stderr,"Can't find main in Echo\n");
     exit(1);
   }
   /*
   ** Allocate argument array
   */
   args = env->NewObjectArray(argc-1,
             env->FindClass("java/lang/String"),NULL);
   if (args == 0)
   {
     fprintf(stderr,"Out of memory\n");
     exit(1);
   }
   /*
   ** Prepare arguments
   */
   for (i=1; i<argc; i++)
   {
     jstring jstr;
     jstr = env->NewStringUTF(argv[i]);
     if (jstr == NULL)
   {
      fprintf(stderr,"Out of memory\n");
      exit(1);
   }
   env->SetObjectArrayElement(args,i-1,jstr);
   }
   /*
   ** Call Java method
   */
   env->CallStaticVoidMethod(cls,mid,args);
   /*
   ** Destroy Java VM
   */
   (*jvm)->DestroyJavaVM(jvm);
   return 0;
}

Compiling the C++ source

You must now compile the above C++ source using the CC command and the correct compiler options. For Echo.cpp, you must also take ASCII mode into account in addition to the default options described above. This example generates an application that can be executed with the X86 variant of JENV on SQ systems:

CC -c -I<installation-path>/include \
      -Kllm_keep,llm_case_lower \ 
      -Kworkspace_stack,c_names_unlimited \ 
      -Kliteral_encoding_ascii -Kno_integer_overflow 
      -D_SNI_THREAD_SUPPORT Echo.cpp

Linking and executing the application

When you link the application, you must remember that the X86 runtime adapter of Java is linked and not the “normal” runtime systems.

You can link the application with the following commands:

export BLSLIB00='$.SKULNK.JENV.090.GREEN-JAVA' 
CC -Kno_link_stdlibs -B llm4 -o Echo \
      Echo.o -l BLSLIB

The program can be called like any other POSIX program on an SQ system. However, for it to run, a X86 variant of JENV must be installed under the default installation path. To use a JENV installed elsewhere, you must set the JAVA_HOME environment variable accordingly (see chapter "Environment variables").

The call using

Echo This is a Java echo

produces the expected output:

This is a Java echo

The program is executed with the default VM described in "Options for selecting the HotSpot™ VM type" in section "java". The VM type can be specified explicitly by setting the JENV_VMTYPE environment variable beforehand. For example:

export JENV_VMTYPE=client
Echo This is a Java echo

This causes the HotSpot™ Client-VM to be used for execution.