JNI and I - Krapf looks at .NET-J2EE interop
Alexander Krapf is president and co-founder of Codemesh. He worked with Hitachi back in the early CORBA days, and since then he has been involved equally in .NET, C++, and Java. We were glad recently to sit down with Alex and discuss some of the ins and outs of interoperability today. We present these in parts, starting with a discussion of early milestones in .NET-Java. That means a look at early JNI.
At the turn of the century, a healthy number of C++ shops were adding servers built in Java. The question often, said Krapf, was how one could leverage established utilities. There were DLLs that were responsible for logging data access – basically a whole slew of things that one could not leverage from the Java side. Krapf saw benefits on use of CORBA in some instances …
“In CORBA you would create an interface description in IDL and then have whatever ORB you were using generate bindings for you in C++ or Java. But it was a layer of abstraction at the component level, not the API level,” he said.
Krapf uncovered a solution, but it was not fully optimal.
JNI rises
“I stumbled over JNI,” he said, pointing to the Java Native Interface. “It was totally unusable if you wanted to create a large scale integration solution. It was too easy to create bugs and too hard to debug them.”
“What was needed was something to make JNI very easy,” he said. He had an idea about code generation. Of course that meant deeply understanding JNI.
JNI is ‘the assembly language of the JVM.’ It is a C interface that lets you call out of Java to C functions. It is part of the Java spec. If you think about it, there has to be something like that. Otherwise, how does Java integrate with native files system services, for example. There has to be some integration layer, and that is essentially what JNI is.
Mosty people avoid it, and correctly so. It is good for them to avoid it. It’s very reliable because it has to be. It is the core of Java sitting on the operating system; it is the core of how you launch Java. How does a JVM start? When you kick of a java.exe, somehow you have to start a JVM. It’s done through a JNI. It’s a way to load a JVM into a native process. It’s done through JNI.
What we said was ‘there is wealth of Java functionality out there. People are starting to write more and more Java software, yet they are sitting on existing C++ software.’ We bet there were enough people that wanted to Java-enable their existing C++ software.”
For example, you have an existing MFC app on Windows and now you want to have it talk to a EJB server, or you want to have it talk to a Java message queue.
What kind of software architects choose this type of solution? We asked. Very often, it seems, it is not exactly a choice.
Said Krapf: “Very often people don’t choose to interoperate; they are forced to interoperate.”
Give me rewrite!
Any time an integration is under consideration, the specter of the rewrite arises. When you have a lot of C++, for example, why not rewrite it in Java. Surely someone will suggest it.
Krapf said it is not likely a favorable course. “When you think of the use of resources required for rewriting software in another language, there may be some reasons to do it, but, from a technology point of view, it is a huge waste of resources.”
“Now you shoulder the burden of the rewrite, and, in the case of C++, there are very likely things in the code that you cannot understand,” he said.
Even ‘broken’ elements may need to be recreated. “There may be bugs your software is counting on,” said Krapf. “Usually you can’t do a one-to-one translation.”
The problem is not unlike the Y2K problem, to again hearken back to the turn of the century, because one encounters legacy code dependencies.
“C++ apps are full of unintended side effects. Things like explicit conversions. They make C++ code so hard to read, unless you step through it in the debugger.”
And such projects seldom go far without individuals suggesting the code can be cleaned up. The specter of refactoring also arises.
Says Krapf: “People refactor, and what they thought was a three month project turns into one year, because they’ll end up completely rewriting the app, rather than porting it.” - By Jack Vaughan
Read Part 1 | Read Part 2 | Read Part 3
July 19th, 2007 at 9:35 am
What we need in the Java world is a higher level interoperability technology that enables us to reference binary components without having to write low-level C functions to bridge the gap between Java classes and C++ (or other language) binary classes.
It would be really neat if the JVM included a binding to Bonobo for Gnone, so that Java classes could simply declare an import attribute (to reference the “.so”) and have the compiler generate the mapping to code. In the binary world, you’d develop Bonobo components using high-level tools.
I guess you’d really want the interface to be agnostic between Bonono and XPCOM so that you’d also be able to integrate in the Mozilla world. Heck, if your interface is agnostic between Bonobo & XPCOM, you’d probably want to cover COM on windows as well.
Hey, here is the solution:
1) Create a JVM interop binding for binary component interop.
2) Create a platform specific implementation that uses COM, XPCOM or Bonobo
3) Add an import attribute to the Java language and extend the compiler.
Why, oh why has nobody thought of it yet?
.. oh yes, silly me.. isn’t that was Microsoft did and didn’t Sun sue to them for it.
“If you like burning bridges.. you should learn to live on an Island”