Showing posts with label Java. Show all posts

[Java] Use C/C++ DLL from Java with JNA

In this article I will create a simple Calculator for example.

- Math function are implemented in DLL (add/sub/mul/dev)

- Use java swing to create GUI

- Use JNA java libary to call math function from DLL


1. Create math C/C++ DLL library

- Open Microsoft Visual Studio

- File > New > Project > Visual C++ > Windows Desktop > Dynamic-Link Library with exports (DLL) 


- Add your source
    Create MyMath.dll with Add/Sub/Mul/Dev function


- Build DLL
    In this test, build Release/x64
    


    MYMATH.dll will be exported to x64/Release folder


2. Download java JNA library
- Donwload load jna-x.x.x.jar and jna-platform-x.x.x.jar from github
    https://github.com/java-native-access/jna
- JNA wikipedia
    https://en.wikipedia.org/wiki/Java_Native_Interface
- JNA example code
    https://www.eshayne.com/jnaex/index.html

3. Create java GUI application window
   Reference below article to create GUI app and add JNA library to project

- Create GUI
- Add jna-x.x.x.jar and jna-platform-x.x.x.jar to project

- Add JNA interface that define function relative with DLL function


- Add click event for Calculate button and insert Add/Sub/Mul/Dev function what defined in JNA interface


- Put MYMATH.dll into bin folder

- Click Run button in eclipse to run up application

4. Export
- Export java project to runnable jar file

- Copy MYMATH.dll to folder where exported jar file

- Double click on Calculator.jar to use application


※NOTE:
If you put all the C# and C++ dll files in the java project directory, an error will occur. The reason is that although java can find the C++ dll file, but the C# dll file cannot be found, you need to add the C# dll The file is placed in the bin folder under the jdk installation directory of the running machine. problem solved.
Repeat again. The resulting dll file should be consistent with the version of the jdk installed on the running machine (x86 or x64), otherwise a call error will occur.






Wednesday, May 5, 2021
Tag :

[Java] GUI Application (Swing on Eclipse)

1. Install Eclipse for java

    Download and install "Eclipse IDE for Java Developers"

2. Create new project

- Open Eclipse

- File > New > Java Project

- Fill project name

- Click Finish button


- New package
    Right click on src > New > Package



- New Application Window (Swing JFrame)
    Right click on package mycompany > New > Other 

    > select Application Window > Next

    > input Name of application window > Finish


- Drag and drop companent to window
Can switch Source <=> Design




Note:
If Design mode do not display, right click on java source file and select 
Open With > WindowBuilder Editor




3. Add external library

    - Create new folder and put jar into there



Right click on project > Properties > Java Build Path > Libraries > Add JARs...


    > select jar files > OK

    
    External jar files was added to Referenced Libraries





4. Export to Runnable JAR file

Right click on project > Export > Java - Runnable JAR file > Next


    
    > select "Launch configuration", input "Export destination" > Finish

    
    JAR file will be exported to Export destination folder, double click to run jar file.
    (All external libararies and other project's resource also put in to Hello.jar export file)



Note: Can use command promt with command below
$ java - jar Hello.jar





- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -