Embed Java into a C++ Application

Embed Java into a C++ application?

You can embed a JVM within your application. Oracle's official reference book has some more details. The synopsis of it is:

#include <jni.h>       /* where everything is defined */

int main() {
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */
vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
/* Get the default initialization arguments and set the class
* path */
JNI_GetDefaultJavaVMInitArgs(&vm_args);
vm_args.classpath = ...;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm, &env, &vm_args);
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("Main");
jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V");
env->CallStaticVoidMethod(cls, mid, 100);
/* We could have created an Object and called methods on it instead */
/* We are done. */
jvm->DestroyJavaVM();
}

You can do far more sophisticated things if you want (e.g. custom class loaders) but that's about it in terms of the bare minimum needed to get a JVM working within your application.

How to embed java into C#

You could try IKVM ?

IKVM.NET is useful for several different software development scenarios. Here is a sampling of some of the possibilities.

  • Drop-in JVM
  • Use Java libraries in your .NET applications
  • Develop .NET applications in Java

Embedding JVM into C++ application: How to link it properly?

Solution for Windows:

  1. write C++ code that:
    1. includes <jni.h> from JDK.
    2. loads "${JRE_HOME}/bin/server/jvm.dll" using LoadLibrary from WinAPI.
    3. gets pointers to JNI_CreateJavaVM using GetProcAddress from WinAPI.
    4. calls JNI_CreateJavaVM and does whatever you want with JVM.
  2. compile it with access to ${JAVA_HOME}/include/**/*.h.
  3. just run compiled program.

Working example here: https://bitbucket.org/kkolyan/j4cpp/src/master/

Embed SWT Composite into C# application

To put your SWT composite inside your winform you can start the SWT application in new process and then use Process.MainWindowHandle to get the child process window handle.

This article provide an example to composite another process.

This link is a specific solution for SWT inside winform and winform inside winform(change the process to start...).

In the solution I also demonstrate a way to communicate between the processes through the std-in/out.(there are a lot of questions about it)

Hint:
The code in the repository is not a production code! (C# and Java)

I just want to provide a demo. when i have time, i will refactor the code...

Interacting with java code from C#

Sorry, you cannot call java code / classes Directly from C# code.

One way of doing this is to wrap up your java classes in a java Web Service and call classes indirectly through that web service interface in your C# code.

Another way is using
javareg.exe which exposes java classes as COM. You can find it at following location:

C:\Program Files\Microsoft VisualStudio\VIntDev98\bin\javareg.exe

Following posts might help as well

  • Calling Java Classes Directly from
    .NET
    (uses runtime bridge)
  • Calling Java from Microsoft.NET

How to embed a Java control on a C# winforms control?

I've rechecked that, looks like IKVM currently hasn't enough support in it's AFT assembly. I think the only way to do what you need is run you java control inside separate process with hidden main window and pass handle of that control to your C# application somehow.


Original answer:

I'm not sure if you can just "add" it, but you can try this direction:

It would be necessary to convert java classes to .Net assemblies via IKVM.

  1. First of all you need to get handle of java control. You can look at this post if you need info on how to do this. I failed at this step because I haven't found Win32DrawingSurface and DrawingSurfaces in IKVM's libraries, maybe it's enough to just create some static getHandle() method on the java side.

  2. Get handle of your parent C# control. It's simple since Control have its Handle property.

  3. After obtaining both handles you can set one of the as parent to another. To do this you need to call SetParent function from the WINAPI.

    [DllImport("user32.dll")]
    private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

Embedding Java SWT/AWT bridge into winform form

The problem is the bridge focus handler. In the java 1.8 version, the focus handler use multithread code and it seem that is it the problem in the java embeded application. We remove the async code and it works fine



Related Topics



Leave a reply



Submit