Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C JNI
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*);  
    HINSTANCE hVM;
    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 ';'
#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 24-Sep-12 9:04am
Edited 24-Sep-12 9:25am
v2

1 solution

Rate this: bad
good
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[^]
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 24-Sep-12 19:29pm
   
Should be enough to get started, my 5.
--SA
pasztorpisti at 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)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2014
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