Attached is an example DLL Maker project that calls a Java function using the Java Native Interface (JNI) from C++. The steps for how this works are outlined below:
1. Install the Java Development Kit (JDK).
- In creating this example, I used OpenJDK installed via Microsoft's special Installer of Visual Studio Code for Java developers.
2. Write a Java program.
- In my example, I created a simple Hello class with an intMethod() as described in IBM's JNI example tutorial.
3. Compile the .java file into a .class file. I did this using the Command Prompt and executing the following command:
javac Hello.java
4. Configure the DLL Maker project to include the JNI library:
- In VC++ Directories > Include Directories, add the following two directories:
- C:\Program Files (x86)\Java\jdk1.8.0_131\include
- C:\Program Files (x86)\Java\jdk1.8.0_131\include\win32
- In Linker > Input > Additional Dependencies, add jvm.lib
- In Linker > Input > Delay Loaded Dlls, add jvm.dll
5. Include jni.h in your code. (Line 51 of mydll.cpp)
6. Create a JVM in your code. (Lines 61-102 of mydll.cpp)
7. Get a handle to the method you want to call and then call it. (Lines 118-126 of mydll.cpp)
8. Connect a User Command in FlexSim to fire that C++ code that executes Java code. See the dll_maker_test_model.fsm included in the attached zipped directory.
* Note: this example was built with 32-bit FlexSim because the JDK I installed was 32-bit. Using a 64-bit JVM is beyond the scope of this simple example and is left as an exercise for the user.