Click here to Skip to main content
15,557,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey hi. This is sandeep.
I am also trying to do the similar kind of work you are doing, like calling a java method inside c. I have lot of errors. Can you please correct me where i am wrong.

Errors mainly in the lines
C++
(CALLBACK *fpCJV)(JavaVM**, void**, JavaVMInitArgs*);  
    HINSTANCE hVM;
    fpCJV CreateJavaVM;
    hVM= LoadLibrary("C:\\Program Files (x86)\\Java\\jre7\\bin\\client\\jvm.dll");

This is whole code. Please somebody help me.

C++
#include <ansi_c.h>
#include <jni.h>
#include <jni_md.h>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif


int _stdcall DLLHeader (void)
{
    return 1;
}
int main()
{
    JavaVMOption options[1];
    JNIEnv *env;
    JavaVM *jvm;
    JavaVMInitArgs vm_args;
    long status;
    jclass cls;
    jmethodID mid;
    jint square;
    jboolean not;

    options[0].optionString = "-Djava.class.path=.";
    memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = 0x00010002;
    vm_args.options = options;
    vm_args.nOptions = 1;
    vm_args.ignoreUnrecognized = JNI_TRUE;
#include <ansi_c.h>
#include "jni.h"
#include "jni_md.h"

    typedef jint    
        (CALLBACK *fpCJV)(JavaVM**, void**, JavaVMInitArgs*);
    // this should contain the path to the JVM DLL file
    HINSTANCE hVM;
    fpCJV CreateJavaVM;
    hVM= LoadLibrary("C:\\Program Files (x86)\\Java\\jre7\\bin\\client\\jvm.dll");
    if (hVM == NULL)
    {
        DWORD dwe = GetLastError();
        return -1;
    }

    CreateJavaVM = (fpCJV)GetProcAddress(hVM, "JNI_CreateJavaVM");
    status = CreateJavaVM(&jvm, (void**)&env, &vm_args);


    //status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

    if (status != JNI_ERR)
    {               
        cls = (*env)->FindClass(env, "TestClass");
        if(cls !=0)
        { mid = (*env)->GetStaticMethodID(env, cls, "intMethod", "(I)I");
        if(mid !=0)
        { square = (*env)->CallStaticIntMethod(env, cls, mid, 5);
        printf("Result of intMethod: %d\n", square);
        }
        }

        (*jvm)->DestroyJavaVM(jvm);
        return 0;
    }
    else
        return -1;
}
Posted
Updated 24-Sep-12 9:25am
v2

1 solution

What about checking out and modifying existing code? How to Call Java Functions from C Using JNI[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Sep-12 19:29pm    
Should be enough to get started, my 5.
--SA
pasztorpisti 24-Sep-12 21:24pm    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900