Java Native Access Platform provides cross-platform mappings and utilities for commonly used platform functions across Windows, macOS, and Linux systems.
—
macOS-specific frameworks including Core Foundation, IOKit for hardware access, Carbon for system events, and other macOS system services. These bindings provide native access to macOS frameworks through JNA interfaces.
Fundamental macOS framework providing basic services like string handling, collections, memory management, and run loops.
/**
* Core Foundation framework functions
*/
public interface CoreFoundation extends Library {
CoreFoundation INSTANCE = Native.load("CoreFoundation", CoreFoundation.class);
// String encoding constants
int kCFStringEncodingUTF8 = 0x08000100;
int kCFStringEncodingASCII = 0x0600;
/**
* Create CFString from C string
* @param alloc Allocator to use (can be null for default)
* @param cStr C string to convert
* @param encoding String encoding (kCFStringEncodingUTF8, etc.)
* @return CFString reference, or null if failed
*/
CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, String cStr, int encoding);
/**
* Create CFString from characters
* @param alloc Allocator to use (can be null for default)
* @param chars Character array
* @param numChars Number of characters
* @return CFString reference, or null if failed
*/
CFStringRef CFStringCreateWithCharacters(CFAllocatorRef alloc, char[] chars, CFIndex numChars);
/**
* Get length of CFString
* @param theString CFString to measure
* @return String length in characters
*/
CFIndex CFStringGetLength(CFStringRef theString);
/**
* Get C string from CFString
* @param theString CFString to convert
* @param buffer Buffer to store C string
* @param bufferSize Buffer size
* @param encoding String encoding
* @return true if successful
*/
boolean CFStringGetCString(CFStringRef theString, byte[] buffer, CFIndex bufferSize, int encoding);
/**
* Release Core Foundation object
* @param cf Object to release
*/
void CFRelease(CFTypeRef cf);
/**
* Retain Core Foundation object
* @param cf Object to retain
* @return Same object with incremented reference count
*/
CFTypeRef CFRetain(CFTypeRef cf);
/**
* Get reference count of object
* @param cf Core Foundation object
* @return Current reference count
*/
CFIndex CFGetRetainCount(CFTypeRef cf);
/**
* Get type identifier for object
* @param cf Core Foundation object
* @return Type identifier
*/
CFTypeID CFGetTypeID(CFTypeRef cf);
/**
* Get type identifier for CFString
* @return CFString type identifier
*/
CFTypeID CFStringGetTypeID();
}
/**
* Core Foundation type definitions
*/
public static class CFTypeRef extends PointerType {
public CFTypeRef() { }
public CFTypeRef(Pointer p) { super(p); }
}
public static class CFStringRef extends CFTypeRef {
public CFStringRef() { }
public CFStringRef(Pointer p) { super(p); }
}
public static class CFAllocatorRef extends CFTypeRef {
public CFAllocatorRef() { }
public CFAllocatorRef(Pointer p) { super(p); }
}
public static class CFIndex extends NativeLong {
public CFIndex() { }
public CFIndex(long value) { super(value); }
}
public static class CFTypeID extends NativeLong {
public CFTypeID() { }
public CFTypeID(long value) { super(value); }
}Usage Examples:
import com.sun.jna.platform.mac.CoreFoundation;
import com.sun.jna.platform.mac.CoreFoundation.*;
// Create a CFString
CFStringRef cfString = CoreFoundation.INSTANCE.CFStringCreateWithCString(
null, "Hello, macOS!", CoreFoundation.kCFStringEncodingUTF8);
if (cfString != null) {
// Get string length
CFIndex length = CoreFoundation.INSTANCE.CFStringGetLength(cfString);
System.out.println("String length: " + length.longValue());
// Convert back to Java string
byte[] buffer = new byte[256];
boolean success = CoreFoundation.INSTANCE.CFStringGetCString(
cfString, buffer, new CFIndex(256), CoreFoundation.kCFStringEncodingUTF8);
if (success) {
String javaString = new String(buffer, "UTF-8").trim();
System.out.println("Retrieved string: " + javaString);
}
// Release the CFString
CoreFoundation.INSTANCE.CFRelease(cfString);
}Hardware communication framework for device access, system information, and hardware management.
/**
* I/O Kit framework functions
*/
public interface IOKit extends Library {
IOKit INSTANCE = Native.load("IOKit", IOKit.class);
// I/O Kit return codes
int kIOReturnSuccess = 0;
int kIOReturnError = 0xe00002bc;
int kIOReturnBadArgument = 0xe00002c2;
/**
* Create matching dictionary for I/O Kit services
* @param name Service name to match
* @return Matching dictionary, or null if failed
*/
CFMutableDictionaryRef IOServiceMatching(String name);
/**
* Get services matching criteria
* @param masterPort Master port (usually 0)
* @param matching Matching dictionary
* @param existing Iterator for matching services
* @return I/O Kit return code
*/
int IOServiceGetMatchingServices(int masterPort, CFDictionaryRef matching,
Pointer /* io_iterator_t */ existing);
/**
* Get next service from iterator
* @param iterator Service iterator
* @return Service object, or 0 if no more services
*/
int /* io_object_t */ IOIteratorNext(int /* io_iterator_t */ iterator);
/**
* Get service property
* @param service Service object
* @param key Property key as CFString
* @return Property value as CFType, or null if not found
*/
CFTypeRef IORegistryEntryCreateCFProperty(int /* io_registry_entry_t */ service,
CFStringRef key, CFAllocatorRef allocator, int options);
/**
* Get service name
* @param service Service object
* @param name Buffer for service name
* @return I/O Kit return code
*/
int IORegistryEntryGetName(int /* io_registry_entry_t */ service, byte[] name);
/**
* Get parent service
* @param service Child service
* @param plane I/O Registry plane name
* @param parent Parent service output
* @return I/O Kit return code
*/
int IORegistryEntryGetParentEntry(int /* io_registry_entry_t */ service, String plane,
IntByReference /* io_registry_entry_t */ parent);
/**
* Release I/O Kit object
* @param object Object to release
* @return I/O Kit return code
*/
int IOObjectRelease(int /* io_object_t */ object);
}
/**
* Core Foundation dictionary reference
*/
public static class CFDictionaryRef extends CFTypeRef {
public CFDictionaryRef() { }
public CFDictionaryRef(Pointer p) { super(p); }
}
public static class CFMutableDictionaryRef extends CFDictionaryRef {
public CFMutableDictionaryRef() { }
public CFMutableDictionaryRef(Pointer p) { super(p); }
}Usage Examples:
import com.sun.jna.platform.mac.IOKit;
import com.sun.jna.platform.mac.CoreFoundation;
import com.sun.jna.ptr.IntByReference;
// Enumerate USB devices
CFMutableDictionaryRef matchingDict = IOKit.INSTANCE.IOServiceMatching("IOUSBDevice");
if (matchingDict != null) {
IntByReference iterator = new IntByReference();
int result = IOKit.INSTANCE.IOServiceGetMatchingServices(0, matchingDict, iterator.getPointer());
if (result == IOKit.kIOReturnSuccess) {
int service;
while ((service = IOKit.INSTANCE.IOIteratorNext(iterator.getValue())) != 0) {
// Get device name
byte[] nameBuffer = new byte[256];
if (IOKit.INSTANCE.IORegistryEntryGetName(service, nameBuffer) == IOKit.kIOReturnSuccess) {
String deviceName = new String(nameBuffer).trim();
System.out.println("USB Device: " + deviceName);
}
// Get device properties
CFStringRef vendorKey = CoreFoundation.INSTANCE.CFStringCreateWithCString(
null, "idVendor", CoreFoundation.kCFStringEncodingUTF8);
CFTypeRef vendorID = IOKit.INSTANCE.IORegistryEntryCreateCFProperty(
service, vendorKey, null, 0);
if (vendorID != null) {
// Process vendor ID...
CoreFoundation.INSTANCE.CFRelease(vendorID);
}
CoreFoundation.INSTANCE.CFRelease(vendorKey);
IOKit.INSTANCE.IOObjectRelease(service);
}
}
}Legacy Carbon framework for system events, hot key registration, and low-level system interaction.
/**
* Carbon framework functions (legacy)
*/
public interface Carbon extends Library {
Carbon INSTANCE = Native.load("Carbon", Carbon.class);
// Event class constants
int kEventClassKeyboard = 1801812322; // 'keyb'
int kEventClassApplication = 1634758764; // 'appl'
// Event kind constants
int kEventHotKeyPressed = 5;
int kEventHotKeyReleased = 6;
/**
* Register global hot key
* @param inHotKeyID Hot key identifier
* @param inHotKey Hot key specification
* @param inTarget Event target (can be null)
* @param inOptions Options (usually 0)
* @param outRef Hot key reference output
* @return OSStatus result code
*/
int RegisterEventHotKey(int inHotKeyID, int inHotKey, Pointer inTarget,
int inOptions, Pointer outRef);
/**
* Unregister global hot key
* @param inHotKey Hot key reference to unregister
* @return OSStatus result code
*/
int UnregisterEventHotKey(Pointer inHotKey);
/**
* Install event handler
* @param inTarget Event target
* @param inHandler Handler function
* @param inNumTypes Number of event types
* @param inList Event type specifications
* @param inUserData User data for handler
* @param outRef Handler reference output
* @return OSStatus result code
*/
int InstallEventHandler(Pointer inTarget, Pointer inHandler, int inNumTypes,
Pointer inList, Pointer inUserData, Pointer outRef);
/**
* Get application event target
* @return Event target for current application
*/
Pointer GetApplicationEventTarget();
/**
* Run application event loop
*/
void RunApplicationEventLoop();
/**
* Quit application event loop
*/
void QuitApplicationEventLoop();
}BSD system calls and low-level system services available on macOS.
/**
* BSD system calls on macOS
*/
public interface SystemB extends Library {
SystemB INSTANCE = Native.load("System", SystemB.class);
/**
* Get system information
* @param name System parameter name
* @param oldp Buffer for current value
* @param oldlenp Size of buffer / returned size
* @param newp New value to set (can be null)
* @param newlen Size of new value
* @return 0 if successful, -1 if failed
*/
int sysctlbyname(String name, Pointer oldp, IntByReference oldlenp,
Pointer newp, size_t newlen);
/**
* Get process information
* @param pid Process ID (0 for current process)
* @param info Process info structure
* @param size Size of info structure
* @return 0 if successful, -1 if failed
*/
int proc_pidinfo(int pid, int flavor, long arg, Pointer info, int size);
/**
* Get host information
* @param host Host port
* @param flavor Information type
* @param host_info Buffer for host information
* @param host_info_cnt Size of buffer
* @return KERN_SUCCESS if successful
*/
int host_info(int host, int flavor, Pointer host_info, IntByReference host_info_cnt);
}Framework for disk mounting, unmounting, and volume management.
/**
* Disk Arbitration framework functions
*/
public interface DiskArbitration extends Library {
DiskArbitration INSTANCE = Native.load("DiskArbitration", DiskArbitration.class);
/**
* Create disk arbitration session
* @param allocator Allocator to use (can be null)
* @return Session reference, or null if failed
*/
DASessionRef DASessionCreate(CFAllocatorRef allocator);
/**
* Create disk object from BSD name
* @param allocator Allocator to use (can be null)
* @param session Disk arbitration session
* @param name BSD device name (e.g., "disk1")
* @return Disk reference, or null if failed
*/
DADiskRef DADiskCreateFromBSDName(CFAllocatorRef allocator, DASessionRef session, String name);
/**
* Get disk description
* @param disk Disk reference
* @return Description dictionary
*/
CFDictionaryRef DADiskCopyDescription(DADiskRef disk);
/**
* Mount disk
* @param disk Disk to mount
* @param path Mount path (can be null for default)
* @param options Mount options
* @param callback Completion callback
* @param context Callback context
*/
void DADiskMount(DADiskRef disk, CFURLRef path, int options,
DADiskMountCallback callback, Pointer context);
/**
* Unmount disk
* @param disk Disk to unmount
* @param options Unmount options
* @param callback Completion callback
* @param context Callback context
*/
void DADiskUnmount(DADiskRef disk, int options,
DADiskUnmountCallback callback, Pointer context);
}
public static class DASessionRef extends CFTypeRef {
public DASessionRef() { }
public DASessionRef(Pointer p) { super(p); }
}
public static class DADiskRef extends CFTypeRef {
public DADiskRef() { }
public DADiskRef(Pointer p) { super(p); }
}macOS implementation of extended file attributes with HFS+ and APFS support.
/**
* macOS extended attributes
*/
public interface XAttr extends Library {
XAttr INSTANCE = Native.load("c", XAttr.class);
// Extended attribute options
int XATTR_NOFOLLOW = 1; // Don't follow symbolic links
int XATTR_CREATE = 2; // Create only (fail if exists)
int XATTR_REPLACE = 4; // Replace only (fail if doesn't exist)
/**
* Set extended attribute
* @param path File path
* @param name Attribute name
* @param value Attribute value
* @param size Value size
* @param position Starting position (usually 0)
* @param options Operation options
* @return 0 if successful, -1 if failed
*/
int setxattr(String path, String name, Pointer value, size_t size, int position, int options);
/**
* Get extended attribute
* @param path File path
* @param name Attribute name
* @param value Buffer for value
* @param size Buffer size
* @param position Starting position (usually 0)
* @param options Operation options
* @return Size of attribute, or -1 if failed
*/
size_t getxattr(String path, String name, Pointer value, size_t size, int position, int options);
/**
* Remove extended attribute
* @param path File path
* @param name Attribute name
* @param options Operation options
* @return 0 if successful, -1 if failed
*/
int removexattr(String path, String name, int options);
/**
* List extended attributes
* @param path File path
* @param namebuf Buffer for attribute names
* @param size Buffer size
* @param options Operation options
* @return Size of name list, or -1 if failed
*/
size_t listxattr(String path, Pointer namebuf, size_t size, int options);
}
/**
* macOS extended attribute utilities
*/
public class XAttrUtil {
/**
* Set string extended attribute
* @param path File path
* @param name Attribute name
* @param value String value
* @throws IOException if operation fails
*/
public static void setXAttr(String path, String name, String value) throws IOException;
/**
* Get string extended attribute
* @param path File path
* @param name Attribute name
* @return Attribute value, or null if not found
* @throws IOException if operation fails
*/
public static String getXAttr(String path, String name) throws IOException;
/**
* List all extended attributes
* @param path File path
* @return Array of attribute names
* @throws IOException if operation fails
*/
public static String[] listXAttrs(String path) throws IOException;
}High-level utility classes providing easier access to macOS functionality.
/**
* High-level I/O Kit utilities
*/
public class IOKitUtil {
/**
* Get system serial number
* @return System serial number string
* @throws IOReturnException if operation fails
*/
public static String getSystemSerialNumber() throws IOReturnException;
/**
* Get system model identifier
* @return Model identifier string (e.g., "MacBookPro15,1")
* @throws IOReturnException if operation fails
*/
public static String getSystemModel() throws IOReturnException;
/**
* Get list of USB devices
* @return List of USB device information
* @throws IOReturnException if enumeration fails
*/
public static List<USBDeviceInfo> getUSBDevices() throws IOReturnException;
/**
* Get thermal state information
* @return Thermal state data
* @throws IOReturnException if operation fails
*/
public static ThermalState getThermalState() throws IOReturnException;
}
/**
* I/O Kit specific exception
*/
public class IOReturnException extends Exception {
private final int returnCode;
public IOReturnException(int returnCode) {
super("I/O Kit operation failed with code: 0x" + Integer.toHexString(returnCode));
this.returnCode = returnCode;
}
public int getReturnCode() {
return returnCode;
}
}
/**
* macOS file utilities
*/
public class MacFileUtils extends FileUtils {
@Override
public boolean moveToTrash(File... files) throws IOException {
// Implementation uses Finder to move files to Trash
// Returns true if all files moved successfully
}
@Override
public boolean hasTrash() {
return true; // macOS always has Trash
}
/**
* Get file type and creator codes (legacy HFS)
* @param file File to examine
* @return Type and creator information
* @throws IOException if operation fails
*/
public static FileTypeInfo getFileTypeInfo(File file) throws IOException;
/**
* Set file type and creator codes (legacy HFS)
* @param file File to modify
* @param type File type code
* @param creator Creator code
* @throws IOException if operation fails
*/
public static void setFileTypeInfo(File file, String type, String creator) throws IOException;
}Complete macOS Integration Example:
import com.sun.jna.platform.mac.*;
public class MacOSExample {
public static void main(String[] args) {
try {
// Get system information using I/O Kit
String serialNumber = IOKitUtil.getSystemSerialNumber();
String model = IOKitUtil.getSystemModel();
System.out.println("System: " + model + " (" + serialNumber + ")");
// Work with Core Foundation strings
CFStringRef cfStr = CoreFoundation.INSTANCE.CFStringCreateWithCString(
null, "Hello macOS", CoreFoundation.kCFStringEncodingUTF8);
// Use Disk Arbitration to get volume info
DASessionRef session = DiskArbitration.INSTANCE.DASessionCreate(null);
if (session != null) {
DADiskRef disk = DiskArbitration.INSTANCE.DADiskCreateFromBSDName(
null, session, "disk1");
if (disk != null) {
CFDictionaryRef description = DiskArbitration.INSTANCE.DADiskCopyDescription(disk);
// Process disk description...
CoreFoundation.INSTANCE.CFRelease(description);
CoreFoundation.INSTANCE.CFRelease(disk);
}
CoreFoundation.INSTANCE.CFRelease(session);
}
// Set extended attributes
XAttrUtil.setXAttr("/tmp/testfile", "com.myapp.comment", "My test file");
String comment = XAttrUtil.getXAttr("/tmp/testfile", "com.myapp.comment");
System.out.println("File comment: " + comment);
// Clean up
CoreFoundation.INSTANCE.CFRelease(cfStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}Install with Tessl CLI
npx tessl i tessl/maven-net-java-dev-jna--jna-platform