473,976 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

COM - java interop

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

Nov 17 '05 #1
2 1349
"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;
}
Nov 17 '05 #2
Thank you very much
Andrea

"William DePalo [MVP VC++]" <wi***********@ mvps.org> ha scritto nel
messaggio news:Oi******** ******@TK2MSFTN GP11.phx.gbl...
"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;
}

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4038
by: Sai Kit Tong | last post by:
Hi, I am developing a new application running on Windows platform that needs to interface with existing legacy code - written in basic C / C++. I am trying to evaluate Java vs C# implementations. Originally, I have the impression that C# should be a clear winner. I started with Java and using the guideline from the book "Java Native Interface". Though complex, the book provide details of the practical implementation and potential...
3
2404
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached response). My first reply directed me to source code migration but I didn't have the source code. The second reply mentioned about .NET interoperability (PInvoke I think) but I MENTIONED THAT I COULDN'T FIND ANY DOCUMENTATION FROM MSDN LIBRARY BASED ON...
4
7034
by: Chris Smith | last post by:
Experienced members, Can someone point me in the direction of utilizing some existing objects that where writing in java. I was given a .jar file with some documentation about the classes within it. I am trying to create a instance of these classes from within vb.net (VS 2003 Framework 1.1). Can someone point me in the direction I need to go or some good documentation?
4
3959
by: Saurabh Sharma | last post by:
Hi I have a Project in C# which makes 4 dlls and takes 2 C# class of the dlls and a string array as as an input and returns a string array. I want to use these dlls in java. The dll also makes a Word Object. Please suggest a method to do it. I know that convert from C# to MC++ to C++ to java But the problem with .netmodule is that its making of one class and not of
1
1355
by: dannywaite7 | last post by:
Hi guys, I'm about to start a new network diagramming project based on a java applet. After researching many current client-side technologies I decided to use an applet for the rich graphics library (drag & drop etc.) and because of its cross browser/platform strengths. I would like to build a backend business tier with .NET (and SQL) because this is what I know best and may want to use various functions outside the java applet.
0
10353
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11408
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11572
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10084
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5153
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.