Java Native Access Platform provides cross-platform mappings and utilities for commonly used platform functions across Windows, macOS, and Linux systems.
—
Core utilities that provide consistent APIs across Windows, macOS, and Linux platforms. These utilities abstract away platform-specific differences and provide unified interfaces for common system operations.
Cross-platform file system operations with platform-specific implementations.
/**
* Abstract base class providing cross-platform file operations
*/
public abstract class FileUtils {
/**
* Get platform-specific implementation of FileUtils
* @return FileUtils instance appropriate for current platform
*/
public static FileUtils getInstance();
/**
* Move files to the system trash/recycle bin
* @param files Files to move to trash
* @throws IOException if trash operation fails
*/
public abstract void moveToTrash(File... files) throws IOException;
/**
* Check if the current platform supports trash functionality
* @return true if trash is available
*/
public abstract boolean hasTrash();
}Platform Implementations:
W32FileUtils - Uses Shell32 API to move files to Recycle BinMacFileUtils - Uses Finder to move files to TrashUsage Examples:
import com.sun.jna.platform.FileUtils;
import java.io.File;
import java.io.IOException;
// Get platform-specific file utilities
FileUtils fileUtils = FileUtils.getInstance();
// Check if trash is supported
if (fileUtils.hasTrash()) {
try {
// Move files to trash
fileUtils.moveToTrash(
new File("temp.txt"),
new File("old_data.csv")
);
System.out.println("Files moved to trash successfully");
} catch (IOException e) {
System.err.println("Failed to move files to trash: " + e.getMessage());
}
} else {
System.out.println("Trash functionality not available on this platform");
}Advanced window effects and manipulation that work across different windowing systems.
/**
* Cross-platform window manipulation utilities
* Provides shape masking, transparency effects, and window information
*/
public class WindowUtils {
/** Use this to clear a window mask */
public static final Shape MASK_NONE = null;
// Window Masking Methods
/**
* Apply a shape mask to a window, making parts transparent
* @param w Window to apply mask to
* @param mask Shape defining visible area (bitmap treated, ignores transparency)
*/
public static void setWindowMask(Window w, Shape mask);
/**
* Apply a shape mask to a heavyweight component
* @param c Component to apply mask to
* @param mask Shape defining visible area
*/
public static void setComponentMask(Component c, Shape mask);
/**
* Apply an icon-based mask to a window
* @param w Window to apply mask to
* @param mask Icon to use as mask (non-transparent pixels included)
*/
public static void setWindowMask(Window w, Icon mask);
// Alpha/Transparency Methods
/**
* Check if window alpha transparency is supported
* @return true if global alpha setting is supported
*/
public static boolean isWindowAlphaSupported();
/**
* Get graphics configuration compatible with alpha compositing
* @return GraphicsConfiguration suitable for alpha operations
*/
public static GraphicsConfiguration getAlphaCompatibleGraphicsConfiguration();
/**
* Set window transparency level (Windows requires sun.java2d.noddraw=true)
* @param w Window to make transparent
* @param alpha Transparency level (1.0f = opaque, 0.0f = transparent)
*/
public static void setWindowAlpha(Window w, float alpha);
/**
* Make window transparent with per-pixel alpha (macOS requires apple.awt.draggableWindowBackground)
* @param w Window to make transparent
* @param transparent true for per-pixel transparency, false for opaque
*/
public static void setWindowTransparent(Window w, boolean transparent);
// Window Information Methods (Windows-only)
/**
* Get icon from window handle (Windows only)
* @param hwnd Window handle (HWND)
* @return BufferedImage of window icon, or null if error
*/
public static BufferedImage getWindowIcon(HWND hwnd);
/**
* Get size of an icon (Windows only)
* @param hIcon Icon handle (HICON)
* @return Dimension of icon, or (0,0) if error
*/
public static Dimension getIconSize(HICON hIcon);
/**
* Get all desktop windows with detailed information (Windows only)
* @param onlyVisibleWindows true to return only visible/non-minimized windows
* @return List of DesktopWindow objects with window information
*/
public static List<DesktopWindow> getAllWindows(boolean onlyVisibleWindows);
/**
* Get window title from handle (Windows only)
* @param hwnd Window handle (HWND)
* @return Window title string, or empty string if error
*/
public static String getWindowTitle(HWND hwnd);
/**
* Get file path of process that owns the window (Windows only)
* @param hwnd Window handle (HWND)
* @return Full file path of executable, or null if error
*/
public static String getProcessFilePath(HWND hwnd);
/**
* Get window location and size (Windows only)
* @param hwnd Window handle (HWND)
* @return Rectangle with window bounds
*/
public static Rectangle getWindowLocationAndSize(HWND hwnd);
}
/**
* Information about a desktop window (Windows only)
*/
public class DesktopWindow {
/**
* Create DesktopWindow with window information
* @param hwnd Window handle
* @param title Window title
* @param filePath Path to executable
* @param locAndSize Window bounds
*/
public DesktopWindow(HWND hwnd, String title, String filePath, Rectangle locAndSize);
/**
* Get the Windows window handle
* @return HWND window handle
*/
public HWND getHWND();
/**
* Get window title
* @return Window title string
*/
public String getTitle();
/**
* Get file path of window's associated application
* @return Full file path string
*/
public String getFilePath();
/**
* Get window bounds
* @return Rectangle defining window position and size
*/
public Rectangle getLocAndSize();
}Usage Examples:
import com.sun.jna.platform.WindowUtils;
import com.sun.jna.platform.DesktopWindow;
import javax.swing.JFrame;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.List;
// Create a JFrame for demonstration
JFrame frame = new JFrame("Test Window");
frame.setSize(300, 200);
frame.setVisible(true);
// Apply transparency
WindowUtils.setWindowAlpha(frame, 0.7f); // 70% opacity
// Create circular mask
Shape circleMask = new Ellipse2D.Double(0, 0, 300, 200);
WindowUtils.setWindowMask(frame, circleMask);
// Get all desktop windows
List<DesktopWindow> windows = WindowUtils.getAllWindows(true);
for (DesktopWindow window : windows) {
System.out.println("Window: " + window.getTitle());
System.out.println(" Path: " + window.getFilePath());
System.out.println(" Bounds: " + window.getBounds());
}Cross-platform access to current keyboard state, allowing applications to check if specific keys are currently pressed.
/**
* Cross-platform keyboard state utilities
* Provides access to the local keyboard state across platforms
* Requires graphics environment (throws HeadlessException on headless systems)
* Note: macOS support is not yet implemented
*/
public class KeyboardUtils {
/**
* Check if a specific key is currently pressed
* @param keycode Key code to check (uses KeyEvent constants like KeyEvent.VK_A, KeyEvent.VK_SHIFT)
* @return true if the specified key is currently pressed
*/
public static boolean isPressed(int keycode);
/**
* Check if a specific key at a specific location is currently pressed
* @param keycode Key code to check (uses KeyEvent constants)
* @param location Key location (uses KeyEvent location constants like KEY_LOCATION_LEFT, KEY_LOCATION_RIGHT)
* @return true if the specified key at the specified location is currently pressed
*/
public static boolean isPressed(int keycode, int location);
}Platform Support:
User32.GetAsyncKeyState()XQueryKeymap()UnsupportedOperationException)Usage Examples:
import com.sun.jna.platform.KeyboardUtils;
import java.awt.event.KeyEvent;
// Check if 'A' key is pressed
boolean aPressed = KeyboardUtils.isPressed(KeyEvent.VK_A);
// Check if left Shift key is specifically pressed
boolean leftShiftPressed = KeyboardUtils.isPressed(KeyEvent.VK_SHIFT, KeyEvent.KEY_LOCATION_LEFT);
// Check if right Control key is specifically pressed
boolean rightCtrlPressed = KeyboardUtils.isPressed(KeyEvent.VK_CONTROL, KeyEvent.KEY_LOCATION_RIGHT);
// Check if any Alt key is pressed (location unknown)
boolean altPressed = KeyboardUtils.isPressed(KeyEvent.VK_ALT);
// Example game input checking
if (KeyboardUtils.isPressed(KeyEvent.VK_W)) {
moveForward();
}
if (KeyboardUtils.isPressed(KeyEvent.VK_A)) {
moveLeft();
}Utilities for converting between enum values and integer representations, with special support for flag-based enums.
/**
* Helper methods to convert between enum values and integer flags/indices
* Supports both simple enum-to-index conversion and flag-based enum sets
*/
public class EnumUtils {
/** Uninitialized integer flag constant */
public static final int UNINITIALIZED = -1;
/**
* Convert enum to its ordinal index
* @param val Enum value to convert
* @return Index position of the enum in its declaration order
*/
public static <E extends Enum<E>> int toInteger(E val);
/**
* Convert integer index back to enum value
* @param idx Index position to retrieve
* @param clazz Enum class type
* @return Enum value at specified index, or null if idx == UNINITIALIZED
*/
public static <E extends Enum<E>> E fromInteger(int idx, Class<E> clazz);
/**
* Convert bitwise OR'd flags to a Set of FlagEnum values
* @param flags Bitwise combination of flag values
* @param clazz FlagEnum class type
* @return Set containing all enum values whose flags are set
*/
public static <T extends FlagEnum> Set<T> setFromInteger(int flags, Class<T> clazz);
/**
* Convert Set of FlagEnum values to combined integer flags
* @param set Set of FlagEnum values to convert
* @return Bitwise OR combination of all flag values in the set
*/
public static <T extends FlagEnum> int setToInteger(Set<T> set);
}
/**
* Image processing utilities for window masking and effects
*/
public class RasterRangesUtils {
/**
* Convert a Shape to raster ranges for window masking
* @param shape Shape to convert
* @param bounds Bounds of the area
* @return Raster ranges suitable for native masking APIs
*/
public static int[] shapeToRasterRanges(Shape shape, Rectangle bounds);
}import com.sun.jna.Platform;
// Check current platform and use appropriate utilities
if (Platform.isWindows()) {
// Windows-specific operations
FileUtils fileUtils = FileUtils.getInstance(); // Returns W32FileUtils
} else if (Platform.isMac()) {
// macOS-specific operations
FileUtils fileUtils = FileUtils.getInstance(); // Returns MacFileUtils
} else if (Platform.isLinux()) {
// Linux-specific operations
FileUtils fileUtils = FileUtils.getInstance(); // Returns Unix FileUtils
}
// Or use cross-platform APIs directly
WindowUtils.setWindowAlpha(myWindow, 0.8f); // Works on all platformsInstall with Tessl CLI
npx tessl i tessl/maven-net-java-dev-jna--jna-platform