Phil BoBo avatar image
Phil BoBo posted Phil BoBo edited

Calling Java from the DLL Maker

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).

2. Write a Java program.

3. Compile the .java file into a .class file. I did this using the Command Prompt and executing the following command:


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.

dll makerjavajni
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.



phil.bobo contributed to this article

Related Articles