.NETGURU
COM - java interop
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.vc.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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

Reply to this message...
 
    
William DePalo [MVP VC++] (VIP)
"Andrea" <andreno_spamalmieri@enmo_spam.m> wrote in message
news:Click here to reveal e-mail address...
> 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[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(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[i]);
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;
}

Reply to this message...
 
 




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification