home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / jni2.cpp < prev    next >
C/C++ Source or Header  |  1998-02-26  |  3KB  |  125 lines

  1. /*
  2.  *  jni2.cpp
  3.  *
  4.  *  This cpp code will create a Java VM 
  5.  *  and call Hello.printHello().
  6.  *
  7.  *  Note that this code can either
  8.  *  be linked with an import library if
  9.  *  STATIC is defined, or load a DLL
  10.  *  during run-time.
  11.  */
  12.  
  13. #include <windows.h>
  14. #include <jni.h>
  15. #include <iostream.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. void main()
  20. {
  21.     JNIEnv *env;
  22.     JavaVM *jvm;
  23.     JDK1_1InitArgs vm_args;
  24.     jclass cls;
  25.     jmethodID mid;
  26.  
  27.     vm_args.version = 0x00010001;  
  28.  
  29. #if STATIC                        // link statically with import lib snjjni.lib or javai.lib
  30.  
  31.     // init vm_args and create Java VM
  32.  
  33.     JNI_GetDefaultJavaVMInitArgs( &vm_args );
  34.     vm_args.classpath = getenv( "CLASSPATH" );
  35.     
  36.     if( JNI_CreateJavaVM( &jvm, &env, &vm_args ) < 0 )
  37.     {
  38.         cout << "error: cannot create Java VM" << endl;
  39.         exit( 1 );        
  40.     }
  41.  
  42. #else                             // link dynamically to runtime (preferred)
  43. #if BYTECODE                      // use bytecode runtime
  44.     char * dllname = "javai.dll";
  45. #else                             // use native runtime
  46.     char * dllname = "snjrt11.dll";
  47. #endif
  48.  
  49.     // load the DLL containing Java VM
  50.  
  51.     HMODULE hm = LoadLibrary( dllname );
  52.     if( hm == 0 )
  53.     {
  54.         cout << "error: cannot load " << dllname << endl;
  55.         exit( 1 );
  56.     }
  57.  
  58.     // get a handle to JNI_GetDefaultJavaVMInitArgs() and init vm_args
  59.  
  60.     jint (JNICALL *f1ptr)( void * );
  61.     
  62.     f1ptr = (jint (JNICALL *)( void * )) GetProcAddress( hm, "JNI_GetDefaultJavaVMInitArgs" );
  63.     if( f1ptr == 0 )
  64.     {
  65.         cout << "error: cannot get JNI_GetDefaultJavaVMInitArgs() address " << endl;
  66.         exit( 1 );
  67.     }
  68.         
  69.     (*f1ptr)( &vm_args );
  70.     
  71.     vm_args.classpath = getenv( "CLASSPATH" );
  72.  
  73.     // get a handle to JNI_CreateJavaVM() and create Java VM
  74.  
  75.     jint (JNICALL *f2ptr)( JavaVM **, JNIEnv **, void * );
  76.  
  77.     f2ptr = (jint (JNICALL *)( JavaVM **, JNIEnv **, void * )) GetProcAddress( hm, "JNI_CreateJavaVM" );
  78.     if( f2ptr == 0 )
  79.     {
  80.         cout << "error: cannot get JNI_CreateJavaVM() address " << endl;
  81.         exit( 1 );
  82.     }    
  83.  
  84.     if( (*f2ptr)( &jvm, &env, &vm_args ) < 0 )
  85.     {
  86.         cout << "error: cannot create Java VM" << endl;
  87.         exit( 1 );        
  88.     }
  89.  
  90. #endif
  91.  
  92.     if( env == 0 )
  93.     {
  94.         cout << "error: JNIenv null pointer" << endl;
  95.         exit( 1 );        
  96.     }
  97.  
  98.     // look for samples.jni2.Hello6 class
  99.  
  100.     cls = env->FindClass( "samples/jni2/Hello6" );
  101.     if( cls == 0 ) 
  102.     {
  103.         cout << "error: cannot find class samples.jni2.Hello6" << endl;
  104.         exit(1);
  105.     }
  106.  
  107.     // get static method Hello6.printHello()
  108.  
  109.     mid = env->GetStaticMethodID( cls, "printHello", "()V" );
  110.     if( mid == 0 ) 
  111.     {
  112.         cout << "error: cannot find samples.jni2.Hello6.printHello()" << endl;
  113.         exit( 1 );
  114.     }
  115.  
  116.     // invoke Hello6.printHello()
  117.  
  118.     env->CallStaticVoidMethod( cls, mid );
  119.     
  120.     // destroy Java VM
  121.  
  122.     jvm->DestroyJavaVM();   
  123. }
  124.  
  125.