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

  • SEARCH
  • Home
  • 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 7513889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:19:30+00:00 2026-05-30T00:19:30+00:00

I use the JNI to start a Java Application out of C. While this

  • 0

I use the JNI to start a Java Application out of C. While this is quickly done as long as I’m using a console Application, whith a Swing-GUI things get a bit tricky.

To keep the application “alive” I use a while loop that just runs as long as GUI is not closed. While this loop runs it always requests, if the application is still running by requesting a boolean value.

    while(javaRunning){
    if(JNI_FALSE == env->CallBooleanMethod(obj, boolMethod))
        javaRunning = false;
}

This value is changed when the Java function WindowClosing(Event) is called to indicate that the user closes the window.

Unfortunately this does not work if I close the window. The C-Application still tries to request the boolean value, even though the window is already closed. It obviously is not able to fetch the boolean before the window closes.
A better approach would be to call the C-Code from Java to inform it about the “WindowClosing” event.

Well as far as I can see, this would be possible if the C-Code is loaded by Java (using a DLL) but not via the invocation interface were C instanciates and starts the Java application.
Maybe anyone knows how to get around with this.

  • 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-05-30T00:19:32+00:00Added an answer on May 30, 2026 at 12:19 am

    Alright after a while of try/error; I just realized that sometimes it can be helpful just to check which methods are available in the JNI (RTFM 😉 )

    Anyway, its not really black magic and yes indeed possible and as some guys voted here theres obviously some interest in this question.

    So what we can do if we want to open a native method to be callable from Java, even if it is in a exec? We have to register it and hand over a function pointer!
    Thats how it looks like in C:

    //this is the function that shall be called from Java code
    void JNICALL setWindowClosed(JNIEnv *env, jobject self, jboolean b){
        statusByte = (b==JNI_TRUE) ? true : false;
    }
    
    int main(){
    JNIEnv* env;
    JavaVM* jvm;
    /*
    create JavaVM and instantiate desired class
    
    JNI_CreateJavaVM(...)
    */    
    jclass cls = env->FindClass("ClassName");
    
    //we have our class - now register our function
    JNINativeMethod nativeMethod;
    nativeMethod.name = "setWindowClosing"; //this is the corresponding name in Java
    nativeMethod.signature = "(Z)V"; //parameter contains a boolean and returns void
    nativeMethod.fnPtr = setWindowClosed; //pointer to our function
    env->RegisterNatives(cls, &nativeMethod, 1); //register native method to Java
    }
    

    In addition we need to place a function with the name above in the Java code:

    private native void setWindowClosing(boolean b);
    

    Thats it – call the function in Java and it will use the implementation in C/C++. If my window closes now my application shuts down properly 🙂

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

Sidebar

Related Questions

I want to use the NSStatusBar object from my Java application using JNI. I
I use JNI calls to load PNG files with Android class Bitmap using this
I want to use JNI on Ubuntu 8.10, using Eclipse and gcc (the standard
I'm starting to learn java's JNI to use with an android device. As I
I am trying to create a JNI wrapper for this windows application called 'Personal
I'm trying to compile a Java library that uses JNI. When I start the
I'm trying to use JNI and getting java.lang.UnsatisfiedLinkError. Unlike the other million questions asked
My Java RCP application prompts the user for username and password upon start-up. How
I'm integrating open source c program with Java program. I'd tried to use JNI
How do we track memory allocation in a Java application that makes use of

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.