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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:03:18+00:00 2026-05-23T07:03:18+00:00

I have coded the following for numerical integration in C++: // integrate.h: #ifdef BUILDING_DLL

  • 0

I have coded the following for numerical integration in C++:

// integrate.h:
#ifdef BUILDING_DLL
#define  DLL_MACRO __declspec(dllexport)
#else
#define  DLL_MACRO __declspec(dllimport)
#endif

extern "C" {
  typedef double (*Function1VariablePtr)(double x);
  double DLL_MACRO integrate(Function1VariablePtr function, double min, double max);
}

// integrate.cpp: 
#include "integrate.h"
double integrate(Function1VariablePtr function, double min, double max) {
  const int n = 1001;
  double dx  = (max - min)/(1.0*(n - 1));
  double sum = 0.0;
  for(int i = 0; i < n; i++) {
    double xmid = min + (i + 0.5)*dx;
    sum += function(xmid)*dx;
  }
  return sum;
}

Now I want to call this function from Java.
I found how I can implement the integration directly in the JNI “bridge” code:

// C++ "bridge" code to from/to Java:  
JNIEXPORT jdouble JNICALL 
Java_IntegrateJApp_JIntegrate(JNIEnv *jnienv, jclass jc,    
                              jdouble xmin, jdouble xmax) {
  jmethodID mid = jnienv->GetStaticMethodID(jc, "Function1D","(D)D");
  if (mid == 0)
    return - 1.0;
  const int n = 1001;
  double dx  = (xmax - xmin)/(1.0*(n - 1));
  double sum = 0.0;
  for(int i = 0; i < n; i++) {
    double xmid = xmin + (i + 0.5)*dx;
    double f = jnienv->CallStaticDoubleMethod(jc, mid, xmid);
    sum += f*dx;
  }
  return sum;
}

// Java code calling "bridge":
class IntegrateJApp { 
  public static void main(String[] args) { 
      System.loadLibrary("JIntegrate");
      double I = JIntegrate(0.0, 2*Math.PI);
      System.out.println( Double.toString(I) ); 
  } 
  public static double Function1D(double x) {
      return Math.sin(x);
  }
  public static native double JIntegrate(double xmin, double xmax);
} 

However I do not want to implement the numeric integration directly in the C++ bridge code, but rather call the code in integrate.cpp.

How do I do this?
The integrate() function inside integrate.cpp requires a function pointer which I do not have.
Is there a way to get a pointer to a function inside Java using JNI?

Thanks in advance for any answers!

  • 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-23T07:03:18+00:00Added an answer on May 23, 2026 at 7:03 am

    One way that can be done is by using a pointer to member function and changing the signature of the integrate function.

    See below the general idea:

    functionwrapper.h

    Declare a function wrapper class.

    class FunctionWrapper
    {
    public:
        typedef double (FunctionWrapper::*Function1VariablePtr)(double x);
    
        FunctionWrapper(JNIEnv*, jclass);
        double compute(double x);
    };
    

    integrate.h

    Eliminate the previous pointer to function typedef and change the method signature to include the wrapper object and a pointer to its member function.

    #include "functionwrapper.h"
    extern "C" {
        double DLL_MACRO integrate(FunctionWrapper*, FunctionWrapper::Function1VariablePtr, double min, double max);
    }
    

    integrate.cpp

    Change the function invocation to a member function invocation.

    #include "integrate.h"
    double integrate(FunctionWrapper* wrapper, FunctionWrapper::Function1VariablePtr function, double min, double max)
    {
        // ...
        sum += (wrapper->*function)(xmid)*dx;
        // ...
    }
    

    return sum;
    }

    JNI “bridge” code:

    Define the wrapper code and define the function that does the actual invocation. Invoke the integrate function directly from the JNI function:

    #include "functionwrapper.h"
    
    FunctionWrapper::FunctionWrapper(JNIEnv *jnienv, jclass jc) : m_jnienv(jnienv), m_jc(jc) {
        m_method= jnienv->GetStaticMethodID(jc, "Function1D","(D)D");
    }
    
    double FunctionWrapper:compute(double x) {
        return m_jnienv->CallStaticDoubleMethod(m_jc, m_method, x);;
    }
    
    // C++ "bridge" code to from/to Java:
    JNIEXPORT jdouble JNICALL
    Java_IntegrateJApp_JIntegrate(JNIEnv *jnienv, jclass jc,
                                  jdouble xmin, jdouble xmax) {
        FunctionWrapper wrapper(jnienv, jc);
        return integrate(&wrapper, &FunctionWrapper::compute, 2, 3);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a csv file with following sample data. o-option(alphabetical) v-value(numerical) number1,o1,v1,o2,v2,o3,v3,o4,v4,o5,v5,o6,v6 number2,o1,v11,o2,v22,o3,v33,o44,v44,o5,v55,o6,v66 and
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have code with the following form: <?php function doSomething{ //Do stuff with MySQL
I'm trying to use opengl in C#. I have following code which fails with
I have the following code in a web.config file of the default IIS site.
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have the following code snippet. $items['A'] = Test; $items['B'] = Test; $items['C'] =
I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
I have the following code: String inputFile = somefile.txt; FileInputStream in = new FileInputStream(inputFile);

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.