Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

COM - java interop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrea - 15 Sep 2004 08:13 GMT
Hi evrybody !
Does  anybody know how to call a java class from a COM component ?
Any suggestion ?
Thanks
Andrea
William DePalo [MVP VC++] - 15 Sep 2004 17:09 GMT
> 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;
}
Andrea - 16 Sep 2004 13:11 GMT
Thank you very much
Andrea

> > Does  anybody know how to call a java class from a COM component ?
>
[quoted text clipped - 43 lines]
> #include <iostream>
> #include "jni.h"

/***************************************************************************
*******
>  This function displays its argument list

****************************************************************************
******/
> void ShowMessage(char const *pstr, ...)
> {
[quoted text clipped - 15 lines]
>  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)
> {
[quoted text clipped - 64 lines]
>  return 0;
> }
J-Integra I - 15 Jul 2005 23:49 GMT
Hi Andrea,

You can use J-Integra for COM to call any Java classes from any COM
components. J-Integra for COM is a pure Java implementation of the DCOM
protocol, making it several times faster than Web Services.

J-Integra for COM Features:
* Access J2EE components from VB, C++, ASPs, etc...
* Access COM components from EJBs, Servlets, JSPs, Applets, etc...
* Pure Java implementation, 100% managed code (no native code required)
* Bi-directional: "Java to COM" and "COM to Java"
* One-sided deployment (no touch on Microsoft client/server side)
* Supports any JVM running on any platform (Windows, Unix, Linux, etc...)
* Optional high-speed native mode for Windows platforms
* Allows Java clients to subscribe to COM events using standard Java
semantics, and COM clients to subscribe to Java events using standard COM
semantics
* Supports both early binding and late binding access to Java/COM components
* Supports the full range of COM Oleautomation types

For a free evaluation, visit our website at http://j-integra.intrinsyc.com/

Regards,

Raymond HE
Intrinsyc Software International, Inc.
J-Integra Interoperability Solutions
http://j-integra.intrinsyc.com/

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.