how to access a method of C++ library (DLL) in java

11,266

I created JavaCPP exactly for that purpose. I'll copy/paste some sample code and explanations from the page:

The most common use case involves accessing some legacy library written for C++, for example, inside a file named LegacyLibrary.h containing this C++ class:

#include <string>

namespace LegacyLibrary {
    class LegacyClass {
        public:
            const std::string& get_property() { return property; }
            void set_property(const std::string& property) { this->property = property; }
            std::string property;
    };
}

To get the job done with JavaCPP, we can easily define a Java class such as this one--although one could use the Parser to produce it from the header file as demonstrated below:

import com.googlecode.javacpp.*;
import com.googlecode.javacpp.annotation.*;

@Platform(include="LegacyLibrary.h")
@Namespace("LegacyLibrary")
public class LegacyLibrary {
    public static class LegacyClass extends Pointer {
        static { Loader.load(); }
        public LegacyClass() { allocate(); }
        private native void allocate();

        // to call the getter and setter functions 
        public native @StdString String get_property(); public native void set_property(String property);

        // to access the member variable directly
        public native @StdString String property();     public native void property(String property);
    }

    public static void main(String[] args) {
        // Pointer objects allocated in Java get deallocated once they become unreachable,
        // but C++ destructors can still be called in a timely fashion with Pointer.deallocate()
        LegacyClass l = new LegacyClass();
        l.set_property("Hello World!");
        System.out.println(l.property());
    }
}

Alternately, we can produce a Java interface by parsing the header file with a config class such as this one:

@Properties(target="LegacyLibrary", value=@Platform(include="LegacyLibrary.h"))
public class LegacyLibraryConfig implements Parser.InfoMapper {
    public void map(Parser.InfoMap infoMap) {
    }
}

And the following build commands:

$ javac -cp  javacpp.jar LegacyLibraryConfig.java
$ java  -jar javacpp.jar LegacyLibraryConfig
$ javac -cp  javacpp.jar LegacyLibrary.java
$ java  -jar javacpp.jar LegacyLibrary

For more complex examples including Maven/IDE integration, check out the JavaCPP Presets!

Share:
11,266
sabith
Author by

sabith

I am currently working as a software engineer in a reputed company in bangalore..

Updated on June 04, 2022

Comments

  • sabith
    sabith about 2 years

    I have one c++ dll file. And I know the methods used in it. I need to call these methods from my java code. I don't have access to modify the DLL file. Please provide me a solution to do this.

  • Annie Kim
    Annie Kim almost 11 years
    Great job! I'm very excited to know your tool JavaCPP cause I encountered similar problem before. I'll spend some time in learning it! Thank you!
  • Annie Kim
    Annie Kim almost 11 years
    BTW, can JavaCPP call the functions in a DLL file directly in java code given that I don't know the detail of the this DLL.
  • Samuel Audet
    Samuel Audet almost 11 years
    @AnnieKim We need at least the header file and a C++ compiler, but everything else is pretty much automatic.
  • Annie Kim
    Annie Kim almost 11 years
    Okay, I got it. Thank you.
  • Yohanes Khosiawan 许先汉
    Yohanes Khosiawan 许先汉 over 10 years
    @SamuelAudet, how can I have the IDE to generate the Java class (LegacyLibrary.java) automatically..? did I miss something from your explanation..?
  • Samuel Audet
    Samuel Audet over 10 years
    @YohanesKhosiawan许先汉 I've added the details. Enjoy!
  • Yohanes Khosiawan 许先汉
    Yohanes Khosiawan 许先汉 over 10 years
    +1 i see, thank you, @SamuelAudet, I've tried it using command prompt, and yes, it generates the file automatically.. but is it possible to automate this generation process from IDE? with just Clean and Build button, with my *.java config classes under src\.. and my header files under lib.. perhaps if you have any experience doing this, please let me know.. thank you for your help~
  • Samuel Audet
    Samuel Audet over 10 years
    @YohanesKhosiawan许先汉 That's what I'm doing with the JavaCPP Presets: Check it out! I've updated my answer with a reference to it.