views:

167

answers:

5

I've inherited a legacy J++ project. I've upgraded this project to standard Sun Java successfully.

However, this project includes a native C++ dll which the Java code talks to via the Microsoft-specific RNI framework.

Needless to say, calling System.loadLibrary("myRniNativeDll") now throws a UnsatisifiedLinkError, saying one of the dependencies couldn't be found.

I'm totally clueless how to migrate a C++ RNI dll to a JNI dll; I've no idea where to begin. I have the C++ source code, but I don't know how to build a JNI dll. Are there any tips/tutorials/online materials you Java experts can point me to?

+2  A: 

If possible, I suggest that you consider avoiding the RNI -> JNI porting problem by rewriting the C++ code in pure Java.

Writing JNI code requires a huge amount of care, and places JVM stability at risk if you get it wrong. My advice is to avoid it if you possibly can.

Stephen C
If the functionality of the RNI code can be implemented in pure Java, I also recommend this suggestion.
monceaux
+1 for a pure Java version. It is most likely impossible since my guess is it glues in Windows-specific stuff.
Thorbjørn Ravn Andersen
Not possible. The DLL talks to custom hardware, and additionally exposes some Windows-specific functionality that you can't access natively in Java.
Judah Himango
So why did you down vote? Is is bad advice to >>CONSIDER<< avoiding native code? Your question does not say (or IMO imply) that that a pure Java solution is impossible.
Stephen C
You're right, my apologies, you weren't aware the code couldn't be rewritten in pure Java. I've changed my vote to an upvote.
Judah Himango
+1  A: 

While I have never used Microsoft's RNI, only Sun's JNI, I would expect that you couldn't directly use an RNI-style DLL as if it were a JNI-style DLL. I am also unaware of anything that would automatically convert or bridge RNI to JNI.

You do not mention if you have the source to the C++ RNI DLL, although I'll assume you do since you mention inheriting the project. If you have any C++/C experience, and have a good idea of what functionality the RNI DLL is providing, it should not be too difficult to move it to JNI. The time invested in porting it would likely pay off for maintaining it moving forward.

The link you provided to the JNI entry on Wikipedia has lots of helpful links on coding with JNI. Without a bit more detail, it is hard to give any other advice.

monceaux
I do have the C++ source code, thanks. I have a general idea of what it's doing. I just need tips on how to build a JNI dll.
Judah Himango
+2  A: 

If the licensing is acceptable (LGPL), Java Native Access provides a rather friendlier FFI than the JNI does; porting may reduce to stripping away the RNI layer and just exposing a simple 'C'-callable API.

Steve Gilham
Thanks! I'm marking this as the correct answer.
Judah Himango
+2  A: 

Take a look at JNA - it might be possible to access the RNI DLL directly that way; or at least it will make the job of porting it much easier than doing JNI manually.

Michael Borgwardt
+1  A: 

Java Native Interface: Programmer's Guide and Specification is the best resource that I've found for writing JNI code. It doesn't cover RNI -> JNI, but it is a great* starting point for learning how JNI works.

*It's actually very dry, and not particularly fun to read, but it's the best I've been able to find.

TwentyMiles