Java Native Access Platform provides cross-platform mappings and utilities for commonly used platform functions across Windows, macOS, and Linux systems.
npx @tessl/cli install tessl/maven-net-java-dev-jna--jna-platform@5.17.0JNA Platform provides comprehensive cross-platform native API access for Java applications. It extends the core Java Native Access (JNA) library with platform-specific functionality and utilities, offering developers a complete abstraction layer for native system calls across Windows, macOS, and Linux platforms without requiring JNI code.
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.17.0</version>
</dependency>import com.sun.jna.platform.FileUtils;
import com.sun.jna.platform.WindowUtils;
import com.sun.jna.platform.KeyboardUtils;
import com.sun.jna.platform.EnumUtils;
import com.sun.jna.platform.dnd.DragHandler;
import com.sun.jna.platform.dnd.DropHandler;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.unix.LibC;
import com.sun.jna.platform.mac.CoreFoundation;import com.sun.jna.platform.FileUtils;
import com.sun.jna.platform.WindowUtils;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
// Cross-platform file operations
FileUtils fileUtils = FileUtils.getInstance();
fileUtils.moveToTrash(new File("temp.txt"));
// Cross-platform window manipulation
WindowUtils.setWindowAlpha(window, 0.5f); // 50% transparency
// Windows-specific system calls (when on Windows)
if (Platform.isWindows()) {
HWND hwnd = User32.INSTANCE.FindWindow(null, "Calculator");
if (hwnd != null) {
User32.INSTANCE.ShowWindow(hwnd, User32.SW_MAXIMIZE);
}
// Get system information
int processId = Kernel32.INSTANCE.GetCurrentProcessId();
System.out.println("Current PID: " + processId);
}JNA Platform is organized around several key architectural components:
FileUtils and WindowUtils provide unified APIs with platform-specific implementationsCore utilities that work across Windows, macOS, and Linux platforms, providing unified APIs for file management, window manipulation, keyboard input, and utility functions.
// File operations
public abstract class FileUtils {
public static FileUtils getInstance();
public abstract void moveToTrash(File... files) throws IOException;
public abstract boolean hasTrash();
}
// Window manipulation and effects
public class WindowUtils {
public static void setWindowMask(Window w, Shape mask);
public static void setWindowAlpha(Window w, float alpha);
public static void setWindowTransparent(Window w, boolean transparent);
public static List<DesktopWindow> getAllWindows(boolean onlyVisible);
public static BufferedImage getWindowIcon(HWND hwnd);
public static String getWindowTitle(HWND hwnd);
}
// Keyboard state access
public class KeyboardUtils {
public static boolean isPressed(int keycode);
public static boolean isPressed(int keycode, int location);
}
// Enum conversion utilities
public class EnumUtils {
public static <E extends Enum<E>> int toInteger(E val);
public static <E extends Enum<E>> E fromInteger(int idx, Class<E> clazz);
public static <T extends FlagEnum> Set<T> setFromInteger(int flags, Class<T> clazz);
public static <T extends FlagEnum> int setToInteger(Set<T> set);
}Comprehensive Windows API bindings including core system functions, user interface operations, graphics, networking, and COM automation.
// Core Windows APIs
public interface Kernel32 extends StdCallLibrary, WinNT, Wincon {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.DEFAULT_OPTIONS);
boolean CreateProcess(String lpApplicationName, String lpCommandLine,
WinBase.SECURITY_ATTRIBUTES lpProcessAttributes,
WinBase.SECURITY_ATTRIBUTES lpThreadAttributes,
boolean bInheritHandles, DWORD dwCreationFlags,
Pointer lpEnvironment, String lpCurrentDirectory,
WinBase.STARTUPINFO lpStartupInfo,
WinBase.PROCESS_INFORMATION lpProcessInformation);
HANDLE OpenProcess(DWORD dwDesiredAccess, boolean bInheritHandle, DWORD dwProcessId);
DWORD GetCurrentProcessId();
DWORD GetLastError();
}
public interface User32 extends StdCallLibrary, WinUser, WinNT {
User32 INSTANCE = Native.load("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
HWND FindWindow(String lpClassName, String lpWindowName);
int GetWindowText(HWND hWnd, char[] lpString, int nMaxCount);
LRESULT SendMessage(HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam);
boolean ShowWindow(HWND hWnd, int nCmdShow);
}POSIX-compliant system calls and Linux-specific functionality including X11 graphics, device management, and file system operations.
// Unix/Linux system calls
public interface LibC extends Library {
LibC INSTANCE = Native.load("c", LibC.class);
int open(String path, int flags);
int close(int fd);
int read(int fd, Buffer buffer, int count);
int write(int fd, Buffer buffer, int count);
int chmod(String path, int mode);
}
// X Window System
public interface X11 extends Library {
X11 INSTANCE = Native.load("X11", X11.class);
Display XOpenDisplay(String display_name);
int XCloseDisplay(Display display);
Window XCreateSimpleWindow(Display display, Window parent, int x, int y,
int width, int height, int border_width,
long border, long background);
}macOS-specific frameworks including Core Foundation, IOKit for hardware access, and Carbon for system events.
// Core Foundation framework
public interface CoreFoundation extends Library {
CoreFoundation INSTANCE = Native.load("CoreFoundation", CoreFoundation.class);
CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, String cStr, int encoding);
void CFRelease(CFTypeRef cf);
CFTypeID CFGetTypeID(CFTypeRef cf);
}
// I/O Kit framework
public interface IOKit extends Library {
IOKit INSTANCE = Native.load("IOKit", IOKit.class);
CFMutableDictionaryRef IOServiceMatching(String name);
int IOServiceGetMatchingServices(int masterPort, CFDictionaryRef matching,
Pointer existing);
}Complete Component Object Model support for Windows automation, including interfaces, utilities, and type library importing.
// Base COM interface
public interface IUnknown {
HRESULT QueryInterface(REFIID riid, PointerByReference ppvObject);
int AddRef();
int Release();
}
// COM automation interface
public interface IDispatch extends IUnknown {
HRESULT GetTypeInfoCount(IntByReference pctinfo);
HRESULT GetTypeInfo(int iTInfo, LCID lcid, PointerByReference ppTInfo);
HRESULT Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags,
DISPPARAMS.ByReference pDispParams, VARIANT.ByReference pVarResult,
EXCEPINFO.ByReference pExcepInfo, IntByReference puArgErr);
}Enhanced drag and drop functionality with cross-platform ghosted images and simplified APIs that extend Java's built-in support.
// Drag handling with ghosted images
public abstract class DragHandler {
protected DragHandler(Component dragSource, int actions);
public static Transferable getTransferable(DropTargetEvent e);
}
// Drop handling with visual feedback
public abstract class DropHandler implements DropTargetListener {
public DropHandler(Component dropTarget, int actions);
public boolean isActive();
public void setActive(boolean active);
}
// Ghosted drag images
public class GhostedDragImage {
public GhostedDragImage(Component dragSource, Icon icon, Point initialScreenLoc, Point cursorOffset);
public void setAlpha(float alpha);
public void move(Point screenLocation);
public void dispose();
}FileUtils.getInstance() - Platform-specific file operationsWindowUtils static methods - Window manipulation and effectsKeyboardUtils static methods - Keyboard state accessEnumUtils static methods - Enum conversion utilitiesDragHandler, DropHandler - Enhanced drag and drop functionalityKernel32.INSTANCE - Core Windows system functionsUser32.INSTANCE - Windows UI and message handlingAdvapi32.INSTANCE - Advanced Windows services and registryCOM.ObjectFactory - COM object creation and automationLibC.INSTANCE - POSIX system callsX11.INSTANCE - X Window System operationsUdev.INSTANCE - Linux device management (Linux only)CoreFoundation.INSTANCE - Core Foundation operationsIOKit.INSTANCE - Hardware and device accessCarbon.INSTANCE - Legacy Carbon framework eventsJNA Platform provides several mechanisms for error handling:
// Windows types
public class WinDef {
public static class HWND extends HANDLE { }
public static class DWORD extends IntegerType { }
public static class WORD extends IntegerType { }
public static class LRESULT extends IntegerType { }
public static class WPARAM extends IntegerType { }
public static class LPARAM extends IntegerType { }
public static class RECT extends Structure {
public int left, top, right, bottom;
}
public static class POINT extends Structure {
public int x, y;
}
}
// Cross-platform window information
public class DesktopWindow {
private HWND hwnd;
private String title;
private String filePath;
private Rectangle bounds;
}