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.
Asked
Active
Viewed 1.2k times
2
-
http://stackoverflow.com/questions/14706193/how-to-access-a-method-of-c-library-dll-from-java – RaceBase Jul 15 '13 at 06:25
-
you can also Google the "JNI". – alexbuisson Jul 15 '13 at 06:30
-
You can't call c++ methods exported in a dll from another language. C++ doesn't have a standardized ABI that permits this in a reliable way. You'll have to 'flatten' the C++ methods down to a C-interface. – greatwolf Jul 15 '13 at 06:37
-
Is there any way to flatten the c++ methods down to a C-Interface without having access to c++ code. – sabith Jul 15 '13 at 06:56
-
With JNA calling DLL method from java has become very easy. Please check out [my earlier posting](http://stackoverflow.com/questions/16460899/how-to-use-net-dll-in-java/16461397#16461397) related tot he same topic. Let me know if still face any issues – Santosh Jul 15 '13 at 07:23
-
@Santosh You can write a C function which calls the C++ code. There is no way to call the C++ without calling the C++ code in some way. – Peter Lawrey Jul 15 '13 at 07:48
-
@PeterLawrey, I think I am missing something here. I thought that OPs case is simply accessing DLL from Java. Not sure if you are referring to JNI (for shared library) way of doing this. The DLL can be directly called from Java code using [**JNA**](http://www.viaboxxsystems.de/java-interoperation-with-a-native-dll-using-jna) (at least for simple methods and provided you know the method definition) . – Santosh Jul 15 '13 at 08:58
-
@Santosh C++ method names are mangled and can require a C++ object, if plain C calls are made, you are right. – Peter Lawrey Jul 15 '13 at 10:57
1 Answers
4
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!

Samuel Audet
- 4,964
- 1
- 26
- 33
-
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 Jul 16 '13 at 09:12
-
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. – Annie Kim Jul 16 '13 at 09:19
-
@AnnieKim We need at least the header file and a C++ compiler, but everything else is pretty much automatic. – Samuel Audet Jul 16 '13 at 09:44
-
@SamuelAudet, how can I have the IDE to generate the Java class (`LegacyLibrary.java`) automatically..? did I miss something from your explanation..? – Yohanes Khosiawan 许先汉 Mar 17 '14 at 05:07
-
-
+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~ – Yohanes Khosiawan 许先汉 Mar 18 '14 at 00:29
-
@YohanesKhosiawan许先汉 That's what I'm doing with the [JavaCPP Presets](https://code.google.com/p/javacpp/wiki/Presets): Check it out! I've updated my answer with a reference to it. – Samuel Audet Mar 18 '14 at 04:30