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

The Archive Base Latest Questions

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

I am currently writting a JNI wrapper for a C++ class and I’m not

  • 0

I am currently writting a JNI wrapper for a C++ class and I’m not sure if what I have done so far is the most correct way.

As I understand it, it is not possible to declare a Java constructor native, so I ended up writing something like:

package log;

public class Logger
{
    private long internalPtr = 0;

    private native long createNativeInstance(String application, String module, String function);

    public Logger(String application, String module, String function)
    {
        this.internalPtr = createNativeInstance(application, module, function);
    }

    public native String getApplication();

    static { System.loadLibrary("log_java"); }
}

Basically, my internalPtr field holds the pointer to my underlying C++ instance and I create it in a pure Java constructor, using the static native method createNativeInstance().

Is this the correct way to do things ?

Another question which I could get an answer for is: “How should I handle the deallocation ?”

My Java skills are extremely limited, so do not hesitate to suggest even the more “obvious” solutions.

Thank you very much.

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

    That is the way I have always implemented native classes which wrap arround a C/C++ object. I think writing a native constructor could be a bit of a pain, so have never tried.

    For de-allocation, you do a simolar thing. Write a C/C++ function which de-allocates the memory for your C/C++ object, and then call this from the java destructor (finalise method). So something like this:

    private native void destroyNativeInstance( long p_internalPtr );
    
    public void finalize()
    {
        destroyNativeInstance( this.internalPtr );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing my own shell as a project for a class, and have
I am currently writing some JNI code to call a class within my JAVA
I am currently writing a Java compiler and have implemented section 15.12.2.7. of the
I am currently writting programs in linux like this: From the command line I
I know that technically, an Interface is used for reading and not writting or
I am currently writing a program's main function(the bare-bone function so far) so far
I am currently busy writting a javascript library. In that library I want to
Currently when I am writting a bundle in that depends on a package, I
I am writing the C++/Native side of a text/VoIP program that utilizes JNI. Currently
I currently want to make my project accessible not only via back-end code (C#,

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.