The is a sandbox environment to investigate how a Bridge between the two runtimes (that is, CoreCLR and JVM) could be designed.
A JVM agent is written using the JVM Tool Interface (JVMTI). The agent is loaded during JVM application start up and initialized via the Agent_OnLoad export. Bridge initialization is done in the following order:
- On application start-up, the JVM loads the agent and prior to VM initialization calls
Agent_OnLoad. - The agent subscribes to the
VMInitcallback. - The JVM is initialized and the
VMInitcallback is called. During the callback theBridgeContextis populated and its memory address is set in astaticfield,s_BridgeContext, on the application's entry class. - The Java application's
main()is entered. - The Java application calls an exported function from a DNNE assembly, passing the value in
s_BridgeContext. - Calling the exported function forces CoreCLR to be activated via DNNE.
- The exported function calls into the .NET class library, passing in a pointer to the the
BridgeContextcontext. - The .NET function calls the
Callbackfield on theBridgeContext, and enters the Bridge.
After the above sequence of events, the JVM, Bridge and .NET library are fully initialized and control of the current thread leaves the Bridge, .NET library and eventually returns from the Java applications main() function - see "Run" section below.
The BridgeContext context contains pointers to jvmtiEnv and JNIEnv instances and a callback into the Bridge that can be expanded as needed. This approach allows for experimentation with GC and type system coordination.
- CMake >= 3.20 - https://cmake.org/download/
- .NET >= 9.0 - https://dotnet.microsoft.com/download
- C++ compiler >= C++ 17 (that is, MSVC, Clang, GCC)
- JDK >= 20 - https://www.oracle.com/java/technologies/downloads/
- Download a JDK an set the
JDKPathenvironment variable to the root (contains thebin/folder). - Add the JDK's
bin/directory to the path. - Confirm that
cmake,dotnet,javacandjavaare on the path.
To build:
dotnet build build.proj
To clean:
dotnet clean build.proj
The -c flag is respected and supports both Debug and Release builds.
Navigate to the output directory.
cd artifacts
cd [Release/Debug]
Run the JavaApp application and pass the agent flag to load the Bridge.
java -agentlib:bridge JavaApp
Linux It may be required to define LD_LIBRARY_PATH if the JVM fails to load the agent.
All It may be required to define the DOTNET_ROOT environment variable if the .NET install is non-standard.
Expected output:
$ java -agentlib:bridge JavaApp
Bridge!VMInit()
JavaApp.main()
DnLib!DnLib.Init.Initialize()
Bridge!DotnetCallback()