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:
- write C++ code that:
- includes
<jni.h>
from JDK. - loads "${JRE_HOME}/bin/server/jvm.dll" using
LoadLibrary
from WinAPI. - gets pointers to
JNI_CreateJavaVM
usingGetProcAddress
from WinAPI. - calls
JNI_CreateJavaVM
and does whatever you want with JVM.
- includes
- compile it with access to
${JAVA_HOME}/include/**/*.h
. - 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.
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.
Get handle of your parent C# control. It's simple since Control have its Handle property.
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
Java Remove Duplicates from an Array
Strange Floating-Point Behaviour in a Java Program
Dealing with "Xerces Hell" in Java/Maven
Design Patterns: Factory VS Factory Method VS Abstract Factory
Differencebetween "Class.Forname()" and "Class.Forname().Newinstance()"
Hibernate Opensession() VS Getcurrentsession()
When Should One Use Final for Method Parameters and Local Variables
Why Is "Extends T" Allowed But Not "Implements T"
Java Byte Array to String to Byte Array
Java Simpledateformat("Yyyy-Mm-Dd'T'Hh:Mm:Ss'Z'") Gives Timezone as Ist
Why to Use Interfaces, Multiple Inheritance VS Interfaces, Benefits of Interfaces
Java 8 Lambdas, Function.Identity() or T->T
What Is the Solution for the N+1 Issue in JPA and Hibernate
Quickly Read the Last Line of a Text File