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 236745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:22:11+00:00 2026-05-11T20:22:11+00:00

Just a simple question: Is it possible to call a java function from c/c++

  • 0

Just a simple question:
Is it possible to call a java function from c/c++ ?

  • 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-11T20:22:11+00:00Added an answer on May 11, 2026 at 8:22 pm

    Yes you can, but it is a little convoluted, and works in a reflective/non type safe way (example uses the C++ api which is a little cleaner than the C version). In this case it creates an instance of the Java VM from within the C code. If your native code is first being called from Java then there is no need to construct a VM instance

    #include<jni.h>
    #include<stdio.h>
    
    int main(int argc, char** argv) {
    
        JavaVM *vm;
        JNIEnv *env;
        JavaVMInitArgs vm_args;
        vm_args.version = JNI_VERSION_1_2;
        vm_args.nOptions = 0;
        vm_args.ignoreUnrecognized = 1;
    
        // Construct a VM
        jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);
    
        // Construct a String
        jstring jstr = env->NewStringUTF("Hello World");
    
        // First get the class that contains the method you need to call
        jclass clazz = env->FindClass("java/lang/String");
    
        // Get the method that you want to call
        jmethodID to_lower = env->GetMethodID(clazz, "toLowerCase",
                                          "()Ljava/lang/String;");
        // Call the method on the object
        jobject result = env->CallObjectMethod(jstr, to_lower);
    
        // Get a C-style string
        const char* str = env->GetStringUTFChars((jstring) result, NULL);
    
        printf("%s\n", str);
    
        // Clean up
        env->ReleaseStringUTFChars(jstr, str);
    
        // Shutdown the VM.
        vm->DestroyJavaVM();
    }
    

    To compile (on Ubuntu):

    g++ -I/usr/lib/jvm/java-6-sun/include \ 
        -I/usr/lib/jvm/java-6-sun/include/linux \ 
        -L/usr/lib/jvm/java-6-sun/jre/lib/i386/server/ -ljvm jnitest.cc
    

    Note: that the return code from each of these methods should be checked in order to implement correct error handling (I’ve ignored this for convenience). E.g.

    str = env->GetStringUTFChars(jstr, NULL);
    if (str == NULL) {
        return; /* out of memory */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use javascript to calculate the hash. See this for an… May 12, 2026 at 10:10 pm
  • Editorial Team
    Editorial Team added an answer Something like #pragma warning(push, disable: 4201) #include <mmsystem.h> #pragma warning(pop) May 12, 2026 at 10:10 pm
  • Editorial Team
    Editorial Team added an answer Responses with status 301 are supposed to be cacheable, and… May 12, 2026 at 10:10 pm

Related Questions

I have a simple question. Is it possible to call a controller method from
Is it possible to pass a lambda expression to a secondary AppDomain as a
Is it possible to use a pixel shader inside a sprite? I have create
I'm about to start working on a ClickOnce app targeted at internal customers for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.