VC++.NET – The Glue To Integrate .NET and Java

Have you ever had the need for Java/.NET Integration ?
Although the main approach is web services interop, sometimes it’s necessary to do some lower level integration.
For example if you have a neat java library for instance for logging purposes which is tightly integrated in your application.

Why not use it within your .NET application as well ?

With VC++.NET and Java Native Interface (JNI) it’s not that complicated.
Just load the Java VM into the .NET process and call the Java methods with the help of unmanaged C++.

Below you can see an example of how to call the method

boolean MyLogger.log(String name)

which resides in C:\temp\mylogger.jar and is part of the package net.pleus. The class uses log4j.


#include “jni.h” // included in the jdk, link to jvm.lib as well

// configure Java VM
JavaVMOption options[3];
options[0].optionString = “-Djava.compiler=NONE”;
options[1].optionString = “-Djava.class.path=C:\\temp\\mylogger.jar;C:\\temp\\log4j.jar”;
options[2].optionString = “-verbose:jni”;

JavaVMInitArgs vm_args;

vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 3;
vm_args.ignoreUnrecognized = JNI_FALSE;

// load Java VM
JavaVM* jvm;
JNIEnv* env;
JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args);

// find class and method
jclass cls = env->FindClass(“net/pleus/MyLogger”);
jmethodID mid = env->GetStaticMethodID(cls,”log”,”(Ljava/lang/String;)Z”);

// call method
jstring name = env->NewStringUTF(“my message”);
bool res = env->CallStaticObjectMethod(cls,mid,name);

// cleanup
jvm->DestroyJavaVM();

The technology stack could be as follows:

Managed Code (C#, VB.NET) -> Hybrid C++.NET and JNI -> Java

I tried it out. It works very well (and fast).
The most difficult thing is to perform the type conversions and code the cumbersome signature descriptions.
The other way round (call .NET from Java) is also possible though it’s a little bit trickier.

Best Regards From ADO.NET

IBM and BEA announced the the new Service Data Object Specification (SDO).
It promises to simplify and unify several Java data access methods.
Most important it introduces support for disconnected data access scenarios.
Therewith it follows the same design principle as ADO.NET.

Below is a comparsion of the main classes (.NET – Java):

DataSet – DataGraph
DataTable – DataObject
DataAdapter – DataMediatorService

It seems that IBM/BEA favours an object centric approach whereas .NET uses a relational approach.
Interesting to see how .NET affects the J2EE development.

On the whole it’s great, as in the past nearly every enterprise level project created it’s own solution to cope with disconnected data structures.

Who Is Defining The Standards ?

In the past the picture was clear.
On one hand there were “open” standards like the J2SE/J2EE maintained via the Java Community Process(JCP) and hosted by Sun Microsystems.
Other vendors participated and helped the standards evolve.
On the other hand were de facto standards, mainly set by Microsoft, for instance MFC or the .NET technology stack.

Currently the picture is changing.
In my perception the pace in the Java world has become slower. How long did it take to finish for instance the EJB2.1 specification ?
In the meantime Microsoft inceased their effort to elaborate their technologies.

Beside that new boards like WS-I to were founded to help establish the WS-* standards. If you have a look at the specifications you will notice that the key players are IBM, Microsoft and BEA.
In addition to that BEA and IBM work closely together to write Java Joint Specifications.

Quote from the announcement:
“In response to requests from customers and joint Independent Software Vendor (ISV) partners, BEA and IBM are collaborating on specifications for programming models and APIs for Java(tm) 2 Enterprise Edition (J2EE) application servers that provide programmers with simpler and more powerful ways of building portable server applications.”
Why not submitting proposals to the JCP first ?

Many of the upcoming standards are mainly driven by IBM, Microsoft and BEA.
In my opinion the benefit is, that it’s no more everyone against Microsoft but everyone for innovation.