Click here to Skip to main content
13,199,680 members (56,234 online)
Rate this:
Please Sign up or sign in to vote.
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
(CALLBACK *fpCJV)(JavaVM**, void**, JavaVMInitArgs*);  
    fpCJV CreateJavaVM;
    hVM= LoadLibrary("C:\\Program Files (x86)\\Java\\jre7\\bin\\client\\jvm.dll");

This is whole code. Please somebody help me.

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

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
    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);
        return 0;
        return -1;
Posted 24-Sep-12 8:04am
Updated 24-Sep-12 8:25am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100