"Andrea" <andreno_spamal mieri@enmo_spam .m> wrote in message
news:OK******** ******@TK2MSFTN GP09.phx.gbl...
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 interoperabilit y 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
interoperabilit y. 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.prin tln("Java says hello, arguments to main() are:");
count = args.length;
for ( i = 0; i < count; i++ )
System.out.prin tln(" '" + args[i] + "'");
}
}
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(cha r 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_1InitAr gs vm_args;
jvm = 0;
env = 0;
helloClass = 0;
vm_args.nVersio n = 0x00010001;
JNI_GetDefaultJ avaVMInitArgs(& vm_args);
result = JNI_CreateJavaV M(&jvm, &env, &vm_args);
if ( result == JNI_ERR )
ShowMessage("Er ror invoking the JVM.", 0);
else
{
helloClass = env->FindClass("Hel lo");
stringClass = env->FindClass("jav a/lang/String");
if ( !helloClass )
ShowMessage("Ca n't find class: 'Hello'.", 0);
else if ( !stringClass )
ShowMessage("Ca n't find Java's string class.", 0);
else
{
mainId = env->GetStaticMetho dID(helloClass, "main",
"([Ljava/lang/String;)V");
if ( !mainId )
ShowMessage("Ca n't find main method.", 0);
else
{
args = env->NewObjectArray (argc, stringClass, 0);
for ( i = 0; i < argc; i++ )
{
arg = env->NewStringUTF(a rgv[i]);
env->SetObjectArray Element(args, i, arg);
}
env->CallStaticVoid Method(helloCla ss, mainId, args);
for ( i = 0; i < argc; i++ )
{
arg = static_cast<jst ring>( env->GetObjectArray Element(args, i) );
env->DeleteLocalRef (arg);
}
env->DeleteLocalRef (args);
}
}
}
return 0;
}