COM - java interop

A

Andrea

Hi evrybody !
Does anybody know how to call a java class from a COM component ?
Any suggestion ?
Thanks
Andrea
 
W

William DePalo [MVP VC++]

Andrea said:
Does anybody know how to call a java class from a COM component ?

Well, this question is a blast from the past. :)

MS' JVM is aware of COM. And interoperability with it and COM is easier than
otherwise. Unfortunately, it is stuck by legal agreement at some time in the
distant past and is unsupported by MS. I don't think that you'll find the MS
Java SDK available anymore.

With any other JVM, the Java Native Interface (JNI) provides the means to
interoperability. Assuminmg that your COM component is written in C or C++
it is not all that hard. You should check a good text or web site for more
info on JNI.

The quick little console hack little below uses JNI to capture its command
line arguments, find and load this little Java class

public class Hello
{
public static void main (String[] args)
{
int i, count;

System.out.println("Java says hello, arguments to main() are:");

count = args.length;

for ( i = 0; i < count; i++ )
System.out.println(" '" + args + "'");
}
}

find its static method named main(), call main() and pass its own arguments
to main. It will get you started.

But if you want to pursue JNI, note though how you do what is called
"Activation" in the Java camp (embedding a JVM in native code) has changed
significantly since I wrote the hack many years ago. You will have to bring
the lines that set the arguments for VM creation and that start the VM in
line with what is required by whatever VM you use.

Regards,
Will

#include <windows.h>
#include <iostream>
#include "jni.h"

/**********************************************************************************
This function displays its argument list
**********************************************************************************/
void ShowMessage(char const *pstr, ...)
{
va_list list;
char const *next;

va_start(list, pstr);

next = pstr;

do
{
std::cout << next;
}
while ( next = va_arg(list, char const *) );

std::cout << std::endl;

va_end(list);
}


/**********************************************************************************
This function initializes the Java VM, searches for the class named
"Hello" and calls it static method main with this function's argument
list. Note that in order to do so, it must allocate an array of
references to Java strings as well as the Java strings themselves. After
the Java method is called the references to the strings and the array are
deleted.
**********************************************************************************/
int main(int argc, char **argv)
{
int i;
long result;
jclass helloClass, stringClass;
JavaVM *jvm;
JNIEnv *env;
jstring arg;
jmethodID mainId;
jobjectArray args;
MS_JDK1_1InitArgs vm_args;

jvm = 0;
env = 0;
helloClass = 0;
vm_args.nVersion = 0x00010001;

JNI_GetDefaultJavaVMInitArgs(&vm_args);

result = JNI_CreateJavaVM(&jvm, &env, &vm_args);

if ( result == JNI_ERR )
ShowMessage("Error invoking the JVM.", 0);

else
{
helloClass = env->FindClass("Hello");
stringClass = env->FindClass("java/lang/String");

if ( !helloClass )
ShowMessage("Can't find class: 'Hello'.", 0);

else if ( !stringClass )
ShowMessage("Can't find Java's string class.", 0);

else
{
mainId = env->GetStaticMethodID(helloClass, "main",
"([Ljava/lang/String;)V");

if ( !mainId )
ShowMessage("Can't find main method.", 0);

else
{
args = env->NewObjectArray(argc, stringClass, 0);

for ( i = 0; i < argc; i++ )
{
arg = env->NewStringUTF(argv);
env->SetObjectArrayElement(args, i, arg);
}

env->CallStaticVoidMethod(helloClass, mainId, args);

for ( i = 0; i < argc; i++ )
{
arg = static_cast<jstring>( env->GetObjectArrayElement(args, i) );
env->DeleteLocalRef(arg);
}

env->DeleteLocalRef(args);
}
}
}

return 0;
}
 
A

Andrea

Thank you very much
Andrea

William DePalo said:
Andrea said:
Does anybody know how to call a java class from a COM component ?

Well, this question is a blast from the past. :)

MS' JVM is aware of COM. And interoperability with it and COM is easier than
otherwise. Unfortunately, it is stuck by legal agreement at some time in the
distant past and is unsupported by MS. I don't think that you'll find the MS
Java SDK available anymore.

With any other JVM, the Java Native Interface (JNI) provides the means to
interoperability. Assuminmg that your COM component is written in C or C++
it is not all that hard. You should check a good text or web site for more
info on JNI.

The quick little console hack little below uses JNI to capture its command
line arguments, find and load this little Java class

public class Hello
{
public static void main (String[] args)
{
int i, count;

System.out.println("Java says hello, arguments to main() are:");

count = args.length;

for ( i = 0; i < count; i++ )
System.out.println(" '" + args + "'");
}
}

find its static method named main(), call main() and pass its own arguments
to main. It will get you started.

But if you want to pursue JNI, note though how you do what is called
"Activation" in the Java camp (embedding a JVM in native code) has changed
significantly since I wrote the hack many years ago. You will have to bring
the lines that set the arguments for VM creation and that start the VM in
line with what is required by whatever VM you use.

Regards,
Will

#include <windows.h>
#include <iostream>
#include "jni.h"

/***************************************************************************
*******
This function displays its argument list
****************************************************************************
******/
void ShowMessage(char const *pstr, ...)
{
va_list list;
char const *next;

va_start(list, pstr);

next = pstr;

do
{
std::cout << next;
}
while ( next = va_arg(list, char const *) );

std::cout << std::endl;

va_end(list);
}


/***************************************************************************
*******
This function initializes the Java VM, searches for the class named
"Hello" and calls it static method main with this function's argument
list. Note that in order to do so, it must allocate an array of
references to Java strings as well as the Java strings themselves. After
the Java method is called the references to the strings and the array are
deleted.
****************************************************************************
******/
int main(int argc, char **argv)
{
int i;
long result;
jclass helloClass, stringClass;
JavaVM *jvm;
JNIEnv *env;
jstring arg;
jmethodID mainId;
jobjectArray args;
MS_JDK1_1InitArgs vm_args;

jvm = 0;
env = 0;
helloClass = 0;
vm_args.nVersion = 0x00010001;

JNI_GetDefaultJavaVMInitArgs(&vm_args);

result = JNI_CreateJavaVM(&jvm, &env, &vm_args);

if ( result == JNI_ERR )
ShowMessage("Error invoking the JVM.", 0);

else
{
helloClass = env->FindClass("Hello");
stringClass = env->FindClass("java/lang/String");

if ( !helloClass )
ShowMessage("Can't find class: 'Hello'.", 0);

else if ( !stringClass )
ShowMessage("Can't find Java's string class.", 0);

else
{
mainId = env->GetStaticMethodID(helloClass, "main",
"([Ljava/lang/String;)V");

if ( !mainId )
ShowMessage("Can't find main method.", 0);

else
{
args = env->NewObjectArray(argc, stringClass, 0);

for ( i = 0; i < argc; i++ )
{
arg = env->NewStringUTF(argv);
env->SetObjectArrayElement(args, i, arg);
}

env->CallStaticVoidMethod(helloClass, mainId, args);

for ( i = 0; i < argc; i++ )
{
arg = static_cast<jstring>( env->GetObjectArrayElement(args, i) );
env->DeleteLocalRef(arg);
}

env->DeleteLocalRef(args);
}
}
}

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top