Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8050947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:11:47+00:00 2026-06-05T07:11:47+00:00

I am trying to call a Java method from the code. C code listens

  • 0

I am trying to call a Java method from the code. C code listens to either Escape, Shift, Ctrl key press, then it calls the Java method telling which key was pressed. Following are the snippets that play a role in this.

C Snippet:

mid = (*env)->GetMethodID(env,cls,"callBack","(Ljava/lang/String;)V");
Env = env;
if(called)
    switch(param) {
        case VK_CONTROL:
            printf("Control pressed !\n");
            (*Env)->CallVoidMethodA(Env,Obj,mid,"11"); // calling the java method
            break;
        case VK_SHIFT:
            printf("Shift pressed !\n");
            (*Env)->CallVoidMethodA(Env,Obj,mid,"10"); // calling the java method
            break;
        case VK_ESCAPE:
            printf("Escape pressed !\n");
            (*Env)->CallVoidMethodA(Env,Obj,mid,"1B"); // calling the java method
            break;
        default:
            printf("The default case\n");
            break;
    }

Java Snippet:

public void callBack(String key) {
    String x = KeyEvent.getKeyText(Integer.parseInt(key, 16));
    System.out.println(x);
}

When I run the program and press the Escape key I get this on the console:

Escape pressed !
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x5c8b809a, pid=7588, tid=8088
#
# JRE version: 7.0
# Java VM: Java HotSpot(TM) Client VM (20.0-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# V  [jvm.dll+0x19809a]
#
# An error report file with more information is saved as:
# W:\UnderTest\NetbeansCurrent\KeyLoggerTester\build\classes\hs_err_pid7588.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

I know I am calling the Java function the wrong way, but I don’t know where I am wrong. As from the output, it satisfies the case when I press the Escape key and then an unexpected error occurs.

Link to the LOG FILE

EDIT:

After the answer by mavroprovato I still get the same errors.

I edited this way:

(*Env)->CallVoidMethodA(Env,Obj,mid,(*Env)->NewStringUTF(Env,"1B"));

EDIT:

COMPLETE CODE version 1

COMPLETE CODE version 2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:11:48+00:00Added an answer on June 5, 2026 at 7:11 am

    The JVM is crashing because the JNIEnv that is used is not a valid one. There are other issues with the code as well.

    The Sun JNI documentation is providing very good information regarding threads.

    Here comes some parts that are obvious:

    Create a JNI_OnLoad function in your code. It will be called when the library is loaded. Then cache the JavaVM pointer because that is valid across threads. An alternative is to call (*env)->GetJavaVM in the initializeJNIVars function but I prefer the first one.

    In your initializeJNIVars you can save the obj reference by calling Obj = (*env)->NewGlobalRef(obj).

    In the LowLevelKeyboardProc you will have to get the env pointer:

    AttachCurrentThread(JavaVM *jvm, JNIEnv &env, NULL);


    Edit

    OK, here are the code that you should add to get it working, I have tried it myself and it works. NB: I have not analyzed what your code is actually doing so I just did some fixes to get it working.

    Add these variables among your other global variables:

    static JavaVM *javaVM = NULL;
    static jmethodID callbackMethod = NULL;
    static jobject callbackObject = NULL;
    

    You can remove your cls, mid, Env and Obj variables and use mine instead.

    Create the JNI_OnLoad method where you cache the JavaVM pointer:

    JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
        JNIEnv *env = 0;
    
        if ((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_4)) {
            return JNI_ERR;
        }
    
        javaVM = jvm;
    
        return JNI_VERSION_1_4;
    }
    

    Alter your initializeJNIVars to look like the following:

    void Java_keylogger_TestKeys_initializeJNIVars(JNIEnv *env, jobject obj) {
        jclass cls = (*env)->GetObjectClass(env,obj);
        callbackMethod = (*env)->GetMethodID(env, cls, "callBack", "(Ljava/lang/String;)V");
        callbackObject = (*env)->NewGlobalRef(env, obj);
        if(cls == NULL || callbackMethod == NULL) {
            printf("One of them is null \n");
        }
        called = TRUE;
    }
    

    And finally in your LowLoevelKeyboardProc code you will have to add the following:

    ...
    WPARAM param = kbhook->vkCode;
    
    JNIEnv *env;
    jint rs = (*javaVM)->AttachCurrentThread(javaVM, (void**)&env, NULL);
    if (rs != JNI_OK) {
        return NULL; // Or something appropriate...
    }
    ...
    
        case VK_ESCAPE:
            printf("Escape pressed !\n");
            jstring message = (*env)->NewStringUTF(env, "1B");
            (*env)->CallVoidMethod(env, callbackObject, callbackMethod, message);
            break;
    ...
    

    In your unregisterWinHook you should delete the global reference so that objects can be GC’d.

    ...
    (*env)->DeleteGlobalRef(env, callbackObject);
    

    And that’s it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a simple Java method call from C++ while Java calls
I'm trying to call the java code from S. This method call: cls =
I'm trying to call a java method from native C code in an Android
I am trying to call my Javascript method from my java in GWT below
i am trying to call a navite method defined in c++ from java, the
I am trying to call a method in a Java applet from JS. I
I am trying to call cleartool from an java application, but cleartool hangs even
I'm trying to call a PostgreSQL stored procedure from a Java app; the procedure
I have been trying to call a mapreduce job from a simple java program
I'm trying to call a stored procedure from my .NET code which has one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.