or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

com.mdcross-platform.mddrag-drop.mdindex.mdmac.mdunix-linux.mdwindows.md
tile.json

tessl/maven-net-java-dev-jna--jna-platform

Java Native Access Platform provides cross-platform mappings and utilities for commonly used platform functions across Windows, macOS, and Linux systems.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/net.java.dev.jna/jna-platform@5.17.x

To install, run

npx @tessl/cli install tessl/maven-net-java-dev-jna--jna-platform@5.17.0

index.mddocs/

JNA Platform

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

Package Information

  • Package Name: jna-platform
  • Package Type: maven
  • Language: Java
  • Maven Group: net.java.dev.jna
  • Installation:
    <dependency>
      <groupId>net.java.dev.jna</groupId>
      <artifactId>jna-platform</artifactId>
      <version>5.17.0</version>
    </dependency>

Core Imports

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;

Basic Usage

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);
}

Architecture

JNA Platform is organized around several key architectural components:

  • Cross-Platform Abstractions: Base classes like FileUtils and WindowUtils provide unified APIs with platform-specific implementations
  • Platform-Specific Libraries: Direct bindings to native APIs (Win32, POSIX, etc.) accessed through singleton instances
  • Utility Wrappers: High-level wrapper classes (*Util classes) that simplify common operations and provide better error handling
  • Type Definitions: Complete data structure definitions matching native types (HWND, DWORD, RECT, etc.)
  • COM Integration: Comprehensive Component Object Model support for Windows automation

Capabilities

Cross-Platform Utilities

Core 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);
}

Cross-Platform Utilities

Windows Platform APIs

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);
}

Windows Platform

Unix/Linux Platform APIs

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);
}

Unix/Linux Platform

Mac Platform APIs

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);
}

Mac Platform

COM Automation (Windows)

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);
}

COM Automation

Drag and Drop Support

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();
}

Drag and Drop Support

Entry Points by Platform

Cross-Platform Entry Points

  • FileUtils.getInstance() - Platform-specific file operations
  • WindowUtils static methods - Window manipulation and effects
  • KeyboardUtils static methods - Keyboard state access
  • EnumUtils static methods - Enum conversion utilities
  • DragHandler, DropHandler - Enhanced drag and drop functionality

Windows Entry Points

  • Kernel32.INSTANCE - Core Windows system functions
  • User32.INSTANCE - Windows UI and message handling
  • Advapi32.INSTANCE - Advanced Windows services and registry
  • COM.ObjectFactory - COM object creation and automation

Unix/Linux Entry Points

  • LibC.INSTANCE - POSIX system calls
  • X11.INSTANCE - X Window System operations
  • Udev.INSTANCE - Linux device management (Linux only)

Mac Entry Points

  • CoreFoundation.INSTANCE - Core Foundation operations
  • IOKit.INSTANCE - Hardware and device access
  • Carbon.INSTANCE - Legacy Carbon framework events

Error Handling

JNA Platform provides several mechanisms for error handling:

  • Win32Exception: Windows-specific errors with GetLastError() integration
  • IOReturnException: macOS IOKit operation errors
  • Native errno: Unix/Linux system call error codes
  • COM exceptions: Automated error handling for COM operations

Common Types

// 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;
}