dcsimg
Some in JNI, how to invoke java method from c program.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sriram_Panigrahi
Posted On:   Thursday, May 23, 2002 02:22 AM

I have some problem with JNI. I like to invoke a JAVA method from c/c++ and catching the return result from the method. I am dumping my code as follow both c nad Java code pls. help me out. sriram ============================================================ C code ============================================================ #include #include #include #include typedef struct { jint (JNICALL *JNI_CreateJavaVM) (JavaVM **pvm, JNIEnv **env, void *args); } InvocationFunctions; #pragma argsused BOOL Handler (DWORD dwCtrlType) { return TRUE; } HINSTANCE LoadJVM (InvocationFunctions *ifn)    More>>

I have some problem with JNI.

I like to invoke a JAVA method from c/c++ and catching the return result from the method. I am dumping my code as follow both c nad Java code pls. help me out.

sriram

============================================================
C code
============================================================
#include
#include
#include
#include
typedef struct
{
jint (JNICALL *JNI_CreateJavaVM) (JavaVM **pvm, JNIEnv **env, void *args);
}
InvocationFunctions;

#pragma argsused
BOOL Handler (DWORD dwCtrlType)
{
return TRUE;
}

HINSTANCE LoadJVM (InvocationFunctions *ifn)
{
HKEY key;
HINSTANCE hinstance;
static BYTE DLLdata [128];
DWORD dwCount = 128, dwType;

if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
"SOFTWARE\JavaSoft\Java Runtime Environment\1.2",
0,KEY_ALL_ACCESS,&key) != ERROR_SUCCESS)
return NULL;
if (RegQueryValueEx (key,"RuntimeLib",NULL,&dwType,DLLdata,&dwCount)!= ERROR_SUCCESS){
RegCloseKey (key);
return NULL;
}

RegCloseKey (key);
if ((hinstance = LoadLibrary ((LPCTSTR) DLLdata)) != NULL)
{
ifn->JNI_CreateJavaVM = (void *)
GetProcAddress (hinstance, "JNI_CreateJavaVM");

if (ifn->JNI_CreateJavaVM == NULL)
{
FreeLibrary (hinstance);
return NULL;
}
}

return hinstance;
}

void main (int argc, char *argv [])
{
int i;
jint ret;
JNIEnv *env;
JavaVM *jvm;
jclass clazz;
jstring jstr;
jint intJ;
jmethodID mid;
HINSTANCE hinstance;
JavaVMOption option;
JavaVMInitArgs vm_args;
jobjectArray str_array;
char buffer [512], *psz;
InvocationFunctions ifn;

SetConsoleCtrlHandler ((PHANDLER_ROUTINE) Handler, TRUE);

if ((hinstance = LoadJVM (&ifn)) == NULL)
return;
vm_args.version = 0x00010002;
vm_args.nOptions = 1;
if ((psz = getenv ("CLASSPATH")) == 0) psz = ".";
wsprintf (buffer, "-Djava.class.path=%s", psz);
option.optionString = buffer;
vm_args.options = &option;


if ((ret = ifn.JNI_CreateJavaVM (&jvm, &env, &vm_args)) < 0)
{
fprintf (stderr, "Can't create JVM. Error: %ld
", ret);
goto main2;
}

if ((jstr = (*env)->NewStringUTF (env, "")) == NULL)
{
fprintf (stderr, "Insufficient memory.
");
goto main1;
}

str_array = (*env)->NewObjectArray (env,
argc - 1,(*env)->FindClass (env, "java/lang/String"),jstr);

if (str_array == NULL)
{
fprintf (stderr, "Insufficient memory.
");
goto main1;
}
for (i = 1; i < argc; i++)
{
if ((jstr = (*env)->NewStringUTF (env, argv [i])) == 0)
{
fprintf (stderr, "Insufficient memory.
");
goto main1;
}

(*env)->SetObjectArrayElement (env, str_array, i - 1, jstr);
}

if ((clazz = (*env)->FindClass (env, "te")) == 0)
{
fprintf (stderr, "Can't locate the te class. Exiting...
");
goto main1;
}

if ((mid = (*env)->GetStaticMethodID (env, clazz, "main","([Ljava/lang/String;)V")) == 0)
{
fprintf (stderr, "Can't locate the main method. Exiting...
");
goto main1;
}
(*env)->CallStaticVoidMethod (env, clazz, mid, str_array);

if ((*env)->ExceptionOccurred (env))
{
(*env)->ExceptionDescribe (env);
goto main1;
}

if ((mid = (*env)->GetMethodID (env, clazz, "getString","()V")) == 0)
{
fprintf (stderr, "Can't locate the main method. Exiting...
");
goto main1;
}

intJ=(*env)->CallIntMethod (env,clazz,mid);
jstr=" Got the data from the method " & charj;

printf("%d",intJ);

if ((*env)->ExceptionOccurred (env))
{
(*env)->ExceptionDescribe (env);
goto main1;
}

if ((*jvm)->DetachCurrentThread (jvm) != 0)
fprintf (stderr, "Could not detach the main thread from the JVM.
");

main1:

(void) (*jvm)->DestroyJavaVM (jvm);

main2:

FreeLibrary (hinstance);
}

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
JAVA CODE
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

public class te{
public void sayHi(){
System.out.println("hi");
}
public static void main(String[] s){
te t= new te();
t.sayHi();
}
public int getInt(){
return 77;
}
}
************************************************************

Please help to solve the problem.

   <<Less

Re: Some in JNI, how to invoke java method from c program.

Posted By:   kanduri_haricharan  
Posted On:   Monday, May 27, 2002 08:22 AM

hi sriram,
I could not read your code,but i have some examples to offer.
Make sure you set the "PATH" of jvm.dll (in
C:jdk1.3.1_01jreinclassic) and include JVM.lib in PROJECT/Settings/LINK
then complie the code


**************************here is the c File ***********

#include
#include
#include
#define MAIN_CLASS "MyApp2"
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
JNIEnv* env;
JavaVM* jvm;
JDK1_1InitArgs vmargs;
jint rc;
jclass cls;
jmethodID mainId;
jint sq=0;
char ch[1];


/* get CLASSPATH environment variable setting */
char* szClasspath = getenv( "CLASSPATH" );
vmargs.version = 0x00010001; /* version 1.1 */
JNI_GetDefaultJavaVMInitArgs( &vmargs ); /* init vmargs */
/* the classpath returned by JNI_GetDefaultJavaVMInitArgs is wrong */
vmargs.classpath = szClasspath;
rc = JNI_CreateJavaVM( &jvm, (void**)&env, &vmargs ); /* create the JVM */
if( rc < 0 )
{
MessageBox(NULL,"JVM not inituialised","JVM",MB_OK);

return 1;
}

/* load the class containing the static main() method */
cls = (*env)->FindClass( env, "check" );
if( cls == 0 )
{
MessageBox(NULL,"Class not found","Class found",MB_OK);

return 1;

}
/* find the main() method */
mainId = (*env)->GetStaticMethodID(env, cls, "square1", "(I)I");
if( mainId == 0 )
{
MessageBox(NULL,"Function not found","NOT Found",MB_OK);

return 1; /* error */
}

sq=(*env)->CallStaticIntMethod(env, cls,mainId, 7); /* call main() */


MessageBox(NULL,itoa(sq,ch,10),"value",MB_OK);

(*jvm)->DestroyJavaVM( jvm ); /* kill the JVM */
return 0;

}
*****************************************************
Java file ,Put in the same directory



public class check
{

public static int square(int i)
{
return i*i;
}
{
About | Sitemap | Contact