Java JNA

Explain how to use the Java Native Access library (JRA) to access native libraries. References: Using JNA to Access Native Dynamic Libraries 1. Introduction Sometimes we need to use native code to implement some functionality: Reusing legacy code written in C/C++ or any other language able to create native code. Accessing system-specific functionality not available in the standard Java runtime. Trace-offs: Can’t directly use static libraries Slower when compared to handcrafted JNI code. => JNA today is probably the best available choice to access native code from Java. 2. JNA Project Setup Add JNA dependency to the project’s pom.xml (latest version of jna-platform) <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna-platform</artifactId> <version>5.6.0</version> </dependency> 3. Using JNA Step1: Create a Java interface that extends JNA’s Library interface to describe the methods and types used when calling the target native code. Step2: Pass this interface to JNA which returns a concrete implementation of this interface that we use to invoke native methods. 3.1. Example 1: Calling methods from the C Standard Library CMath.java import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; public interface CMath extends Library { CMath INSTANCE = Native.load(Platform.isWindows() ? "msvcrt" : "c", CMath.class); double cosh(double value); } load() method takes two arguments, the return a concrete implementation of this interface, allowing to call any of its methods. The dynamic lib name The Java interface describing the methods that we’ll use We don’t have to add the .so (Linux) or .dll(Window) extension as they’are implied. Also, for Linux-based systems, we don’t need to specific the lib prefix. Main.java public class Main { public static void main(String[] args) { System.out.println(CMath.INSTANCE.cosh(180)); } } 3.2. Example 2: Basic types Mapping char => byte short => short wchar_t => char int => int long => com.sun.jna.NativeLong long long => long float => float double => double char * => String JNA provides the NativeLong type, which uses the proper type depending on the system’architecture. 3.3. Structures and Unions Given this C struct and union struct foo_t { int field1; int field2; char *field3; }; union foo_u { String foo; double bar; }; Java peer class would be @FieldOrder({"field1","field2","field3"}) public class FooType extends Structure { int field1; int field2; String field3; }; public class MyUnion extends Union { public String foo; public double bar; }; MyUnion u = new MyUnion(); u.foo = "test"; u.setType(String.class); lib.some_method(u); Java requires the @FiledOrder annotation so it can properly serialize data into a memory buffer before using it as an argument to the target method.

November 21, 2024 · 2 min · Phong Nguyen

Java Basic IO

Explain how to use basic I/O. References: Basic I/O Reading and writing files in Java (Input/Output) - Tutorial 1. I/O Streams An I/O Stream represents and input source or an output destination. A stream is a sequence of data. input stream: is used to read data from source, once item at a time. outputs stream: is used to write data to a destination, once item |at a time. --stream--> 0101010101... ----------> java.io package ...

November 14, 2024 · 6 min · Phong Nguyen

Java Platform Standard Edition

Java is a programming language created by James Gosling from Sun Microsystems (Sun) in 1991. Java allows to write a program and run it on multiple operating systems. References: Java Platform Standard Edition 8 Documentation Introduction to Java programming - Tutorial 1. Introduction to Java Oracle has two products implement Java SE: JDK (Java SE Development Kit): is a superset of JRE. JRE (Java SE Runtime Environment): provides the libraries, the Java Virtual Machine (JVM) and other components. 1.1. Development Process with Java Source code is first written in .java file. Those source files are then compiled into .class files by the Java compiler( javac). A .class file contains bytecodes(the machine language of the Java VM). The .class file will be run by Java tool as an instance of the Java VM. The Java virtual machine (JVM) is a software implementation of a computer that executes programs like a real machine. Because the Java VM is available on many different OS, the same .class files are capable of running on many OS too. ...

November 12, 2024 · 5 min · Phong Nguyen

Model Context Protocol (MCP)

1. Introduce Modern applications are integrating with Large Language Models (LLMs) to build AI solution. The AI model’s response will be more context-ware if we provide it external source like databases, file systems, pictures ,… MCP provides a standard way to connect AI-powered applications with external data sources. 2. Model Context Protocol Components overview: MCP follows a client-server architecture, that included: MCP provides two transport channels, with many transport types: ...

February 10, 2026 · 2 min · Phong Nguyen

GTK4

Gtk4 with Cpp. References: Gtk 1. Introduction Gtk4 is a widget toolkit for creating graphical user interface (UI). It works on many UNIX-like platform, Windows and Macos. 2. Setup the environment Note: - This is the guides for Windows machine using Windows Subsystem for Linux (WSL). - [W]: The action will be performed on Windows Machine. - [U]: The action will be performed on WSL (in my case, it named "Ubuntu") on the host Windows Machine. - (Guides: ...): The reference link that I refer the step. - (Not try yet): This is referred from the guides but I was not in that case, so that I could not try. ------------------------------- A. Installing from packages ------------------------------- 1. [L] Install required packages. 1.1. [L] Debian/Ubuntu: sudo apt install libgtk-4-1 libgtk-4-dev gtk-4-examples 2. [L] Verify installation. 2.1. [L] gtk version - Run: pkg-config --modversion gtk4 2.2. [L] examples - Run: gtk4-demo ------------------------------- B. (Optional) Build from Source ------------------------------- 1. [L] Install build tools. 1.1. [L] sudo apt install build-essential meson ninja-build \ libglib2.0-dev libpango1.0-dev libgdk-pixbuf-2.0-dev \ libatk1.0-dev libepoxy-dev libgirepository1.0-dev 2. [L] Clone GTK source. 2.1. [L] git clone https://gitlab.gnome.org/GNOME/gtk.git 2.2. [L] cd gtk 3. [L] Build with Meson + Ninja. 3.1. [L] meson setup builddir 3.2. [L] ninja -C builddir 4. [L] (Optional) Install system-wide. 4.1. [L] sudo ninja -C builddir install 3. HelloWorld Create a new file named hello-world-gtk4.c with following content: #include <gtk/gtk.h> static void print_hello (GtkWidget *widget, gpointer data) { g_print ("Hello World\n"); } static void activate (GtkApplication *app, gpointer user_data) { GtkWidget *window; GtkWidget *button; window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "Hello"); gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); button = gtk_button_new_with_label ("Hello World"); g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); gtk_window_set_child (GTK_WINDOW (window), button); gtk_window_present (GTK_WINDOW (window)); } int main (int argc, char **argv) { GtkApplication *app; int status; app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return status; } - Hints: ...

August 21, 2025 · 2 min · Phong Nguyen