### Install libffi Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md Type 'make install' to install the library and header files. ```bash make install ``` -------------------------------- ### Makeinfo Command Not Found Error Example Source: https://github.com/java-native-access/jna/blob/master/www/RaspberryPiDevelopmentEnvironment.md This warning indicates that 'makeinfo' is missing, which is required for generating documentation. Install the 'texinfo' package to resolve this. ```bash makeinfo: command not found ... WARNING: 'makeinfo' is missing on your system. ``` -------------------------------- ### Install Build Dependencies (Debian/Ubuntu) Source: https://github.com/java-native-access/jna/blob/master/www/Contributing.md Install necessary build tools on Debian-based systems using apt-get. ```bash % apt-get install git ant openjdk-6-jdk make autotools gcc ``` -------------------------------- ### Install Development Tools with Homebrew Source: https://github.com/java-native-access/jna/blob/master/www/MacDevelopmentEnvironment.md Installs essential build tools like ant, autoconf, automake, libtool, and gettext using Homebrew. ```bash brew install ant autoconf automake libtool gettext ``` -------------------------------- ### X11/Xlib.h Header Not Found Error Example Source: https://github.com/java-native-access/jna/blob/master/www/RaspberryPiDevelopmentEnvironment.md This error signifies that X11 development libraries are not installed. Install 'libx11-dev' and 'libxt-dev' to fix this issue. ```bash fatal error: X11/Xlib.h: No such file or directory ``` -------------------------------- ### Configure libffi with specific installation prefix Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md Use the --prefix configure switch to specify a custom installation directory for libffi libraries and header files. Defaults to /usr/local. ```bash ./configure --prefix=/path/to/install ``` -------------------------------- ### Install Build Tools and X11 Libraries on Raspberry Pi Source: https://github.com/java-native-access/jna/blob/master/www/RaspberryPiDevelopmentEnvironment.md Installs necessary build tools and X11 libraries required for compiling JNA on Raspberry Pi. Ensure you have internet connectivity and sudo privileges. ```bash sudo apt-get install autoconf automake libtool libx11-dev libxt-dev texinfo ``` -------------------------------- ### Winspool Utility API Example Source: https://github.com/java-native-access/jna/blob/master/www/Contributing.md Abstract class for Winspool utility functions. This example shows the signature for retrieving printer information. ```java /** * Winspool Utility API. * @author dblock[at]dblock.org */ public abstract class WinspoolUtil { public static PRINTER_INFO_1[] getPrinterInfo1() { } } ``` -------------------------------- ### Troubleshooting Missing Build Tools Source: https://github.com/java-native-access/jna/blob/master/www/MacDevelopmentEnvironment.md Illustrates build errors that occur when essential tools like autoconf, automake, or libtool are missing. These can be installed using Homebrew. ```bash $ brew install autoconf automake libtool gettext ``` ```bash native: [exec] Generating configure [exec] ./autogen.sh: line 2: exec: autoreconf: not found ``` ```bash [exec] Can't exec "aclocal": No such file or directory at /usr/local/Cellar/autoconf/2.69/share/autoconf/Autom4te/FileUtils.pm line 326. [exec] autoreconf: failed to run aclocal: No such file or directory ``` ```bash [exec] configure.ac:41: error: possibly undefined macro: AC_PROG_LIBTOOL ``` ```bash native: [exec] Configuring libffi (x86_64) [exec] configure: WARNING: unrecognized options: --enable-static, --disable-shared, --with-pic [exec] configure: error: cannot find install-sh, install.sh, or shtool in ``` -------------------------------- ### JNA Example in JRuby Source: https://github.com/java-native-access/jna/blob/master/www/DynamicallyTypedLanguages.md This example shows how to use JNA from JRuby to access C standard library functions like puts, printf, open, write, fopen, fprintf, fflush, and fclose. It dynamically retrieves functions from the 'c' native library. ```jruby require 'java' module Libc @@lib = com.sun.jna.NativeLibrary.getInstance("c") @@ptr_funcs = [ 'fopen', 'malloc', 'calloc' ] def self.method_missing(meth, *args) if @@ptr_funcs.include?(meth.to_s) @@lib.getFunction(meth.to_s).invokePointer(args.to_java) else @@lib.getFunction(meth.to_s).invokeInt(args.to_java) end end O_RDONLY = 0 O_WRONLY = 1 O_RDWR = 2 end Libc.puts("puts from libc") Libc.printf("Hello %s, from printf\n", "World") file = Libc.open("/dev/stdout", 1, Libc::O_WRONLY) n = Libc.write(file, "Test\n", 5) puts "Wrote #{n} bytes via Libc" path = "/dev/stdout" fp = Libc.fopen(path, "w+") Libc.fprintf(fp, "fprintf to %s via stdio\n", path) Libc.fflush(fp) Libc.fclose(fp) ``` -------------------------------- ### Run libffi tests Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md Execute 'make check' to verify that libffi is functioning correctly. This requires DejaGNU to be installed. ```bash make check ``` -------------------------------- ### Initialize Visual Studio Build Environment for ARM64 Target on x64 Host Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Execute the vcvarsall.bat script to set up the environment for building ARM64 applications on a 64-bit (x64) host using the Visual Studio C++ compiler. Adjust the path to vcvarsall.bat based on your Visual Studio installation. ```cmd "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64_arm64 ``` -------------------------------- ### Initialize Visual Studio Build Environment for x64 Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Execute the vcvarsall.bat script to set up the environment for building 64-bit (x64) applications using the Visual Studio C++ compiler. Adjust the path to vcvarsall.bat based on your Visual Studio installation. ```cmd "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 ``` -------------------------------- ### Solaris Environment Setup for JNA Native Library Build Source: https://github.com/java-native-access/jna/blob/master/www/BuildingNativeLibraries.md Sets up the environment variables for building JNA native libraries on Solaris, ensuring the correct make utility and path are configured. ```bash # Ensure `gmake` is used export MAKE=gmake # Place local binaries on the path export PATH=$HOME/local/bin:$PATH ``` -------------------------------- ### Initialize Visual Studio Build Environment for x86 Target on x64 Host Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Execute the vcvarsall.bat script to set up the environment for building 32-bit (x86) applications on a 64-bit (x64) host using the Visual Studio C++ compiler. Adjust the path to vcvarsall.bat based on your Visual Studio installation. ```cmd "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x64_x86 ``` -------------------------------- ### Build JNA for FreeBSD x86 Source: https://github.com/java-native-access/jna/blob/master/www/FreeBSD.md Steps to build the JNA native library for FreeBSD x86. This involves setting up a FreeBSD VM, installing dependencies, and compiling the library using Ant. ```bash # Fetch image wget https://download.freebsd.org/releases/VM-IMAGES/13.2-RELEASE/i386/Latest/FreeBSD-13.2-RELEASE-i386.qcow2.xz ``` ```bash xz -d FreeBSD-13.2-RELEASE-i386.qcow2.xz ``` ```bash # Ensure there is enough space in the image qemu-img resize -f qcow2 FreeBSD-13.2-RELEASE-i386.qcow2 +5G ``` ```bash # Launch image qemu-system-i386 -m 3096M -drive file=FreeBSD-13.2-RELEASE-i386.qcow2 ``` ```bash gpart show /dev/ada0 gpart recover /dev/ada0 gpart show /dev/ada0 gpart resize -i 4 /dev/ada0 growfs / ``` ```bash # Exit single user mode (BSD boots to multi-user) exit ``` ```bash # Login as root ``` ```bash # Set keyboard configuration kbdmap ``` ```bash # Set current date and time (YYYYMMDDHHMM) date 202403081928 ``` ```bash # Install prerequisites - part 1 - java, build system, rsync pkg install openjdk17 wget automake rsync gmake gcc bash texinfo # Adjust fstab (optional, only needed if reboot is planned) # fdesc /dev/fd fdescfs rw 0 0 # proc /proc procfs rw 0 0 ``` ```bash # Install prerequisites - part 2 - ant wget https://dlcdn.apache.org/ant/binaries/apache-ant-1.10.14-bin.zip unzip apache-ant-1.10.14-bin.zip ``` ```bash # Transfer JNA source code to build environment rsync -av --exclude=.git USER@BUILD_HOST:src/jnalib/ jnalib/ ``` ```bash # Build JNA and run unittests cd jnalib chmod +x native/libffi/configure native/libffi/install-sh /root/apache-ant-1.10.14/bin/ant ``` ```bash # Copy jna native library back to host system scp lib/native/freebsd-x86.jar USER@BUILD_HOST:src/jnalib/lib/native ``` -------------------------------- ### Build JNA for FreeBSD aarch64 Source: https://github.com/java-native-access/jna/blob/master/www/FreeBSD.md Steps to build the JNA native library for FreeBSD aarch64. This involves setting up a FreeBSD VM, installing dependencies, and compiling the library using Ant. ```bash # Fetch FreeBSD 13.2 image and extract it wget https://download.freebsd.org/releases/VM-IMAGES/13.2-RELEASE/aarch64/Latest/FreeBSD-13.2-RELEASE-arm64-aarch64.qcow2.xz ``` ```bash xz -d FreeBSD-13.2-RELEASE-arm64-aarch64.qcow2.xz ``` ```bash # Ensure there is enough space in the image qemu-img resize -f qcow2 FreeBSD-13.2-RELEASE-arm64-aarch64.qcow2 +5G ``` ```bash # Launch aarch64 emulator with downloaded image qemu-system-aarch64 -m 4096M -cpu cortex-a57 -M virt \ -bios /usr/lib/u-boot/qemu_arm64/u-boot.bin \ -serial telnet::4444,server -nographic \ -drive if=none,file=FreeBSD-13.2-RELEASE-arm64-aarch64.qcow2,id=hd0 \ -device virtio-blk-device,drive=hd0 \ -device virtio-net-device,netdev=net0 \ -netdev user,id=net0 ``` ```bash # Connect to terminal for emulated system and boot into single user mode with default shell telnet localhost 4444 ``` ```bash # Resize partitions gpart show /dev/vtbd0 gpart recover /dev/vtbd0 gpart show /dev/vtbd0 gpart resize -i 3 /dev/vtbd0 growfs / ``` ```bash # Exit single user mode (BSD boots to multi-user) exit ``` ```bash # Login as root ``` ```bash # Set current date and time (YYYYMMDDHHMM) date 202403081928 ``` ```bash # Install prerequisites - part 1 - java, build system, rsync pkg install openjdk17 wget automake rsync gmake gcc bash texinfo # Adjust fstab (optional, only needed if reboot is planned) # fdesc /dev/fd fdescfs rw 0 0 # proc /proc procfs rw 0 0 ``` ```bash # Install prerequisites - part 2 - ant wget https://dlcdn.apache.org/ant/binaries/apache-ant-1.10.14-bin.zip unzip apache-ant-1.10.14-bin.zip ``` ```bash # Transfer JNA source code to build environment rsync -av --exclude=.git USER@BUILD_HOST:src/jnalib/ jnalib/ ``` ```bash # Build JNA and run unittests cd jnalib chmod +x native/libffi/configure native/libffi/install-sh /root/apache-ant-1.10.14/bin/ant ``` ```bash # Copy jna native library back to host system scp lib/native/freebsd-aarch64.jar USER@BUILD_HOST:src/jnalib/lib/native ``` -------------------------------- ### Generating COM Bindings with TlbImp Source: https://github.com/java-native-access/jna/blob/master/www/PlatformLibrary.md Example command to generate JNA bindings for COM interfaces using the TlbImp tool. This is used for the first approach of COM binding. ```shell java -cp "dist/jna.jar;dist/jna-platform.jar" com.sun.jna.platform.win32.COM.tlb.TlbImp -tlb.id {50A7E9B0-70EF-11D1-B75A-00A0C90564FE} -tlb.major.version 1 -tlb.minor.version 0 -bind.mode dispId -output.dir outputdir ``` -------------------------------- ### Autoreconf Not Found Error Example Source: https://github.com/java-native-access/jna/blob/master/www/RaspberryPiDevelopmentEnvironment.md This error message indicates that 'autoreconf' is missing, which is part of the autotools package. Install the necessary build tools to resolve this. ```bash ./autogen.sh: 2: exec: autoreconf: not found ``` -------------------------------- ### Build JNA for FreeBSD x86-64 Source: https://github.com/java-native-access/jna/blob/master/www/FreeBSD.md Steps to build the JNA native library for FreeBSD x86-64. This involves setting up a FreeBSD VM, installing dependencies, and compiling the library using Ant. ```bash # Fetch image wget https://download.freebsd.org/releases/VM-IMAGES/13.2-RELEASE/amd64/Latest/FreeBSD-13.2-RELEASE-amd64.qcow2.xz ``` ```bash xz -d FreeBSD-13.2-RELEASE-amd64.qcow2.xz ``` ```bash # Ensure there is enough space in the image qemu-img resize -f qcow2 FreeBSD-13.2-RELEASE-amd64.qcow2 +5G ``` ```bash # Launch image qemu-system-amd64 -m 4096M -drive file=FreeBSD-13.2-RELEASE-amd64.qcow2 ``` ```bash gpart show /dev/ada0 gpart recover /dev/ada0 gpart show /dev/ada0 gpart resize -i 4 /dev/ada0 growfs / ``` ```bash # Exit single user mode (BSD boots to multi-user) exit ``` ```bash # Login as root ``` ```bash # Set keyboard configuration kbdmap ``` ```bash # Set current date and time (YYYYMMDDHHMM) date 202403081928 ``` ```bash # Install prerequisites - part 1 - java, build system, rsync pkg install openjdk17 wget automake rsync gmake gcc bash texinfo # Adjust fstab (optional, only needed if reboot is planned) # fdesc /dev/fd fdescfs rw 0 0 # proc /proc procfs rw 0 0 ``` ```bash # Install prerequisites - part 2 - ant wget https://dlcdn.apache.org/ant/binaries/apache-ant-1.10.14-bin.zip unzip apache-ant-1.10.14-bin.zip ``` ```bash # Transfer JNA source code to build environment rsync -av --exclude=.git USER@BUILD_HOST:src/jnalib/ jnalib/ ``` ```bash # Build JNA and run unittests cd jnalib chmod +x native/libffi/configure native/libffi/install-sh /root/apache-ant-1.10.14/bin/ant ``` -------------------------------- ### Map and Call C Standard Library printf Source: https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md This example demonstrates mapping the C standard library's printf function and calling it from Java. Ensure jna.jar is in your CLASSPATH. ```java package com.sun.jna.examples; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; /** Simple example of JNA interface mapping and usage. */ public class HelloWorld { // This is the standard, stable way of mapping, which supports extensive // customization and mapping of Java to native types. public interface CLibrary extends Library { CLibrary INSTANCE = (CLibrary) Native.load((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class); void printf(String format, Object... args); } public static void main(String[] args) { CLibrary.INSTANCE.printf("Hello, World\n"); for (int i=0;i < args.length;i++) { CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]); } } } ``` -------------------------------- ### Troubleshooting Xcode Command Line Tools Error Source: https://github.com/java-native-access/jna/blob/master/www/MacDevelopmentEnvironment.md Addresses build errors related to missing Xcode when only command line tools are installed. Requires installing the full Xcode application. ```bash native: [exec] xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance ``` -------------------------------- ### Register Native Library for Direct Mapping Source: https://github.com/java-native-access/jna/blob/master/www/DirectMapping.md Register the native library using `Native.register()` within a static initializer. This example demonstrates mapping `cos` and `sin` functions from the C library. ```java import com.sun.jna.*; public class HelloWorld { public static native double cos(double x); public static native double sin(double x); static { Native.register(Platform.C_LIBRARY_NAME); } public static void main(String[] args) { System.out.println("cos(0)=" + cos(0)); System.out.println("sin(0)=" + sin(0)); } } ``` -------------------------------- ### Set JAVA_HOME for x86_64 JDK Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Set the JAVA_HOME environment variable to point to the root of the target x86_64 JDK installation. This is required for the build process. ```cmd set JAVA_HOME=C:\Program Files\AdoptOpenJDK\jdk-8.0.222.10-hotspot ``` -------------------------------- ### AIX Environment Setup for JNA Native Library Build Source: https://github.com/java-native-access/jna/blob/master/www/BuildingNativeLibraries.md Sets up the environment variables for building JNA native libraries on AIX, ensuring the correct make utility and ARFLAGS are used. ```bash # Ensure `gmake` is used export MAKE=gmake # Place local binaries on the path export PATH=/home/$USER/local/bin:$PATH # Ensure working ARFLAGS are used export ARFLAGS='-X32_64 cru' ``` -------------------------------- ### Add Msi Functions to Win32 Platform Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md This snippet shows the addition of Windows Installer (MSI) functions to the platform.win32.Msi interface. These are used for querying component paths and product codes. ```java Add to `platform.win32.Msi`: `MsiGetComponentPath`, `MsiLocateComponent`, `MsiGetProductCode`, `MsiEnumComponents`. ``` -------------------------------- ### Set JAVA_HOME for x86 JDK Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Set the JAVA_HOME environment variable to point to the root of the target x86 JDK installation. This is required for the build process. ```cmd set JAVA_HOME=C:\Program Files (x86)\AdoptOpenJDK\jdk-8.0.222.10-hotspot ``` -------------------------------- ### Add Ant to PATH Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Add the bin directory of the Ant installation to the system's PATH environment variable. This makes the 'ant' command accessible from the command line. ```cmd set PATH=%USERPROFILE%\apache-ant-1.9.11\bin;%PATH% ``` -------------------------------- ### Update Ole32#CoCreateGuid and Ole32#CLSIDFromString Signatures Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md The signature for `com.sun.jna.platform.win32.Ole32#CoCreateGuid` changed from `(GUID.ByReference pguid)` to `(GUID pguid)`. Similarly, `com.sun.jna.platform.win32.Ole32#CLSIDFromString` changed from `(WString lpsz, CLSID.ByReference pclsid)` to `(String lpsz, CLSID.ByReference pclsid)`. ```java com.sun.jna.platform.win32.Ole32#CoCreateGuid(GUID.ByReference pguid) com.sun.jna.platform.win32.Ole32#CoCreateGuid(GUID pguid) com.sun.jna.platform.win32.Ole32#CLSIDFromString(WString lpsz, CLSID.ByReference pclsid) com.sun.jna.platform.win32.Ole32#CLSIDFromString(String lpsz, CLSID.ByReference pclsid) ``` -------------------------------- ### Configure libffi help Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md To view all available configuration options, run 'configure --help'. ```bash configure --help ``` -------------------------------- ### Add SetupApi Functions to Win32 Platform Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md This snippet details the integration of SetupAPI functions into the platform.win32.SetupApi interface. These are used for device information management on Windows. ```java Add to `platform.win32.SetupApi`: `SetupDiGetClassDevs`, `SetupDiDestroyDeviceInfoList`, `SetupDiEnumDeviceInterfaces`, `SetupDiGetDeviceInterfaceDetail`, `SetupDiGetDeviceRegistryProperty`. ``` -------------------------------- ### Build libffi Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md After configuration, type 'make' to build the library. Ensure you are using GNU make. ```bash make ``` -------------------------------- ### Get Environment Variable in JNA Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md Use `platform.win32.Kernel32.GetEnvironmentVariable` or `platform.win32.Kernel32Util.getEnvironmentVariable` to retrieve environment variables on Windows systems. ```java platform.win32.Kernel32.GetEnvironmentVariable ``` ```java platform.win32.Kernel32Util.getEnvironmentVariable ``` -------------------------------- ### Get Field Order for Structures in JNA Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md Use `Structure.getFieldOrder()` to retrieve the correct field names for structures. This method supersedes `Structure.setFieldOrder()` and is now required. ```java Structure.getFieldOrder() ``` -------------------------------- ### Build JNA Project (Unix-like) Source: https://github.com/java-native-access/jna/blob/master/www/Contributing.md Clone the JNA repository and build the project on most Unix-like systems. ```bash % git clone git@github.com:java-native-access/jna % ant dist test test-platform ``` -------------------------------- ### Enable Native Access for Classpath Loading (JDK 24+) Source: https://github.com/java-native-access/jna/blob/master/src/com/sun/jna/overview.html Use this command when JNA is loaded from the classpath to enable native access, preparing for future JDK restrictions on JNI. ```bash java --enable-native-access=ALL-UNNAMED -jar jna.jar ``` -------------------------------- ### Build libffi from git sources Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md If building libffi directly from git, run ./autogen.sh first to generate the configure script. This requires autoconf, automake, and libtool. ```bash ./autogen.sh ``` -------------------------------- ### Set Android NDK Platform and PATH Source: https://github.com/java-native-access/jna/blob/master/www/AndroidDevelopmentEnvironment.md Configure environment variables for the Android NDK platform and add toolchain binaries to the system's PATH. This is necessary for the native build process. ```bash export NDK_PLATFORM=/home/matthias/bin/android-ndk-r12b/platforms/android-21 export PATH=$NDK_PLATFORM/../../toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/:$PATH export PATH=$NDK_PLATFORM/../../toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/:$PATH export PATH=$NDK_PLATFORM/../../toolchains/mips64el-linux-android-4.9/prebuilt/linux-x86_64/bin/:$PATH export PATH=$NDK_PLATFORM/../../toolchains/mipsel-linux-android-4.9/prebuilt/linux-x86_64/bin/:$PATH export PATH=$NDK_PLATFORM/../../toolchains/x86-4.9/prebuilt/linux-x86_64/bin/:$PATH export PATH=$NDK_PLATFORM/../../toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin/:$PATH ``` -------------------------------- ### Set JAVA_HOME for aarch64 JDK Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Set the JAVA_HOME environment variable to point to the root of the target aarch64 JDK installation. This is for native builds; for cross-compiling, use the x86_64 setting. ```cmd set JAVA_HOME=%USERPROFILE%\jdk-16-ea+19-windows-aarch64 ``` -------------------------------- ### Use TypeMapper for Library-Wide Custom Conversions Source: https://context7.com/java-native-access/jna/llms.txt Configure a TypeMapper for a library to handle custom type conversions globally. This example shows mapping Java Boolean to native Integer. ```java public interface MyLibrary extends Library { TypeMapper MAPPER = new DefaultTypeMapper() { { addTypeConverter(Boolean.class, new TypeConverter() { public Object fromNative(Object value, FromNativeContext ctx) { return ((Integer) value) != 0; } public Object toNative(Object value, ToNativeContext ctx) { return ((Boolean) value) ? 1 : 0; } public Class nativeType() { return Integer.class; } }); } }; Map OPTIONS = new HashMap() { { put(Library.OPTION_TYPE_MAPPER, MAPPER); } }; MyLibrary INSTANCE = Native.load("mylib", MyLibrary.class, OPTIONS); // Boolean now maps to native int automatically boolean isReady(); } ``` -------------------------------- ### Copy JNA Native Library to Host Source: https://github.com/java-native-access/jna/blob/master/www/FreeBSD.md Use this command to copy the FreeBSD x86-64 JNA native library JAR file to the specified location on the build host. ```bash scp lib/native/freebsd-x86-64.jar USER@BUILD_HOST:src/jnalib/lib/native ``` -------------------------------- ### Get Global Variable Address with JNA Source: https://github.com/java-native-access/jna/blob/master/src/com/sun/jna/overview.html Obtain the address of a global variable using NativeLibrary.getGlobalVariableAddress. The address is returned as a Pointer, which can then be used to read or write the variable's value. ```java Pointer globalVarPtr = NativeLibrary.getGlobalVariableAddress("my_global_var"); ``` -------------------------------- ### Compile and Run Unit Tests with Ant Source: https://github.com/java-native-access/jna/blob/master/www/MacDevelopmentEnvironment.md Compiles the JNA project and runs unit tests for the current architecture using the Ant build tool. ```bash ant ``` -------------------------------- ### Configure libffi for 64-bit Windows with MSVC Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md For 64-bit Windows builds using MSVC, append the -m64 flag to the CC and CXX compiler commands. Ensure the --build option is set appropriately. ```bash path/to/configure CC="path/to/msvcc.sh -m64" CXX="path/to/msvcc.sh -m64" LD=link CPP="cl -nologo -EP" CPPFLAGS="-DFFI_BUILDING_DLL" ``` -------------------------------- ### Build libffi static library for ARM64 with MSVC Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md Static library builds for ARM64 with MSVC are available in the msvc_build folder. Use the provided Visual Studio solution file and header files. ```bash aarch64/Ffi_staticLib.sln ``` -------------------------------- ### Support Zero-Length REG_MULTI_SZ on Win32 Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md This fix enables proper support for getting and setting zero-length `REG_MULTI_SZ` values on Windows (Win32). This addresses issues with registry manipulation involving multi-string values. ```c Properly support getting and setting zero-array-length `REG_MULTI_SZ` values on Win32. ``` -------------------------------- ### Enable Debug Mode in MSVC Build Script Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Toggle debug mode in the `native/libffi/msvcc.sh` script to get more verbose output for troubleshooting MSVC build issues. This helps in identifying specific compilation or linking problems. ```diff - verbose= + verbose=1 ``` -------------------------------- ### Platform Detection and File Utilities with JNA Source: https://context7.com/java-native-access/jna/llms.txt Detects the current operating system and its architecture, and demonstrates cross-platform file operations like moving files to the trash. Requires the jna-platform library. ```java import com.sun.jna.Platform; import com.sun.jna.platform.FileUtils; import com.sun.jna.platform.FileMonitor; import java.io.File; import java.io.IOException; public class PlatformUtilsExample { public static void main(String[] args) throws Exception { // Platform detection System.out.println("OS: " + System.getProperty("os.name")); System.out.println("Is Windows: " + Platform.isWindows()); System.out.println("Is Mac: " + Platform.isMac()); System.out.println("Is Linux: " + Platform.isLinux()); System.out.println("Is 64-bit: " + Platform.is64Bit()); System.out.println("C Library: " + Platform.C_LIBRARY_NAME); // Cross-platform file utilities FileUtils fileUtils = FileUtils.getInstance(); // Check if trash is supported if (fileUtils.hasTrash()) { System.out.println("Trash supported on this platform"); // Move file to trash instead of permanent delete File tempFile = File.createTempFile("test", ".txt"); tempFile.deleteOnExit(); // fileUtils.moveToTrash(tempFile); // System.out.println("Moved to trash: " + tempFile); } // File monitoring (cross-platform) FileMonitor monitor = FileMonitor.getInstance(); File watchDir = new File(System.getProperty("user.home")); monitor.addWatch(watchDir); monitor.addFileListener(new FileMonitor.FileListener() { public void fileChanged(FileMonitor.FileEvent e) { System.out.println("File changed: " + e.getFile()); } public void fileDeleted(FileMonitor.FileEvent e) { System.out.println("File deleted: " + e.getFile()); } public void fileCreated(FileMonitor.FileEvent e) { System.out.println("File created: " + e.getFile()); } }); System.out.println("Monitoring: " + watchDir); Thread.sleep(5000); // Watch for 5 seconds monitor.removeWatch(watchDir); System.out.println("Stopped monitoring"); } } ``` -------------------------------- ### Build Native Package for Older JDKs (Solaris x86) Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md Instructions for building JNA's native code package for platforms with JDK versions lower than 8. This involves using Ant with specific properties and then building on the target system. ```bash ant -Dbuild.os.name=SunOS -Dbuild.os.arch=x86 native-build-package ``` ```bash bash build.sh ``` -------------------------------- ### Cross-Compile for Alternate Architectures with Ant Source: https://github.com/java-native-access/jna/blob/master/www/MacDevelopmentEnvironment.md Cross-compiles JNA for different macOS architectures using Ant by setting the 'os.prefix' property. ```bash # ARM64 (>= 11.0) ant -Dos.prefix=darwin-aarch64 ``` ```bash # x86 (<= 10.13) ant -Dos.prefix=darwin-x86 ``` ```bash # x86_64 ant -Dos.prefix=darwin-x86-64 ``` ```bash # PPC (<= 10.5) ant -Dos.prefix=darwin-ppc ``` ```bash # PPC64 ant -Dos.prefix=darwin-ppc64 ``` -------------------------------- ### Build OpenBSD x86 Native Libraries Source: https://github.com/java-native-access/jna/blob/master/www/BuildingNativeLibraries.md Steps to build JNA native libraries for OpenBSD x86. This involves transferring build packages, setting Java and make environments, and executing the build script. ```bash scp user@BUILD_HOST:src/jnalib/build/build-package-openbsd-x86-7.0.2.zip . unzip build-package-openbsd-x86-7.0.2.zip cd build-package-openbsd-x86-7.0.2 export JAVA_HOME=/usr/local/jdk-1.8.0 export MAKE=gmake sh build.sh scp openbsd-x86.jar user@BUILD_HOST:src/jnalib/lib/native ``` -------------------------------- ### Configure MSVC Environment Variables in Bash Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Set MSVC, WSDK, and WSDK_64 environment variables and configure INCLUDE and LIB paths for cross-compiling on Windows using bash. Ensure cygpath is used for path conversion. ```bash export MSVC="/c/Program Files (x86)/Microsoft Visual Studio 10.0/vc" export WSDK="/c/Program Files (x86)/Microsoft SDKs/Windows/v7.0A" export WSDK_64="/c/Program Files/Microsoft SDKs/Windows/v7.1" export INCLUDE="$(cygpath -m "$MSVC")/include;$(cygpath -m "$WSDK")/include" # for x86_64 target export LIB="$(cygpath -m "$MSVC")/lib/amd64;$(cygpath -m "$WSDK_64")/lib/x64" # for x86 target export LIB="$(cygpath -m "$MSVC")/lib;$(cygpath -m "$WSDK")/lib" ``` -------------------------------- ### Build JNA Project Source: https://github.com/java-native-access/jna/blob/master/www/Contributing.md Build the JNA project using Ant. This command builds the distribution, runs tests, and tests the platform-specific libraries. ```bash ant dist test test-platform ``` -------------------------------- ### Compile with Alternate macOS SDK using Ant Source: https://github.com/java-native-access/jna/blob/master/www/MacDevelopmentEnvironment.md Specifies an older macOS SDK root for compilation when using Ant. ```bash ant -DSDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk ``` -------------------------------- ### Configure libffi for Windows with clang-cl Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md To build libffi on Windows using the LLVM clang-cl compiler, specify the -clang-cl flag for CC and CXX. ```bash path/to/configure CC="path/to/msvcc.sh -clang-cl" CXX="path/to/msvcc.sh -clang-cl" LD=link CPP="clang-cl -EP" ``` -------------------------------- ### Build OpenBSD x86-64 Native Libraries Source: https://github.com/java-native-access/jna/blob/master/www/BuildingNativeLibraries.md Steps to build JNA native libraries for OpenBSD x86-64. This involves transferring build packages, setting Java and make environments, and executing the build script. ```bash scp user@BUILD_HOST:src/jnalib/build/build-package-openbsd-x86-64-7.0.2.zip . unzip build-package-openbsd-x86-7.0.2.zip cd build-package-openbsd-x86-7.0.2 export JAVA_HOME=/usr/local/jdk-1.8.0 export MAKE=gmake sh build.sh scp openbsd-x86-64.jar user@BUILD_HOST:src/jnalib/lib/native ``` -------------------------------- ### Run Ant Build with Detailed Output Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Execute the Ant build process with detailed output enabled using the `-DEXTRA_MAKE_OPTS="--debug=v"` flag. This provides extensive logging to help diagnose native compilation and linking errors. ```bash ant -DEXTRA_MAKE_OPTS="--debug=v" ``` -------------------------------- ### Load and Synchronize Kernel32 Instance Source: https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md Load the kernel32 library and optionally create a synchronized instance to limit native calls to one at a time. This is useful for thread safety. ```java Kernel32 INSTANCE = (Kernel32) Native.load("kernel32", Kernel32.class); // Optional: wraps every call to the native library in a // synchronized block, limiting native calls to one at a time Kernel32 SYNC_INSTANCE = (Kernel32) Native.synchronizedLibrary(INSTANCE); ``` -------------------------------- ### Build Dragonfly BSD Native Libraries Source: https://github.com/java-native-access/jna/blob/master/www/BuildingNativeLibraries.md Use this command to synchronize JNA source code, set execute permissions, and build native libraries on Dragonfly BSD. ```bash rsync -av --exclude=.git USER@BUILD_HOST:src/jnalib/ jnalib/ chmod +x jnalib/native/libffi/configure jnalib/native/libffi/install-sh cd jnalib ant ``` -------------------------------- ### JNA Mapping and Usage of ByReference Source: https://github.com/java-native-access/jna/blob/master/www/ByRefArguments.md Demonstrates the equivalent JNA mapping for the C allocate_buffer function and its usage with PointerByReference and IntByReference to capture returned values. Ensure necessary JNA classes are imported. ```java // Equivalent JNA mapping void allocate_buffer(PointerByReference bufp, IntByReference lenp); // Usage PointerByReference pref = new PointerByReference(); IntByReference iref = new IntByReference(); lib.allocate_buffer(pref, iref); Pointer p = pref.getValue(); byte[] buffer = p.getByteArray(0, iref.getValue()); ``` -------------------------------- ### Enable Native Access for Modulepath Loading (JDK 24+) Source: https://github.com/java-native-access/jna/blob/master/src/com/sun/jna/overview.html Use this command when JNA is loaded from the modulepath to enable native access for a specific module, addressing JDK 24+ security changes. ```bash java --enable-native-access=com.sun.jna -p jna-jpms.jar -m com.sun.jna ``` -------------------------------- ### Configure libffi for Windows with MSVC Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md Use the msvcc.sh wrapper script for configuring libffi on Windows with Microsoft Visual C++ compiler. Specify CC, CXX, LD, and CPP flags as shown. ```bash path/to/configure CC=path/to/msvcc.sh CXX=path/to/msvcc.sh LD=link CPP="cl -nologo -EP" CPPFLAGS="-DFFI_BUILDING_DLL" ``` -------------------------------- ### Solaris x86 64-bit JNA Native Library Build Source: https://github.com/java-native-access/jna/blob/master/www/BuildingNativeLibraries.md Builds the 64-bit JNA native library for Solaris x86 by configuring libffi, building, and copying the resulting JAR. ```bash # Reference the 64bit JKD 8 export JAVA_HOME=/opt/csw/java/jdk1.8.0_201 # Build cd build-package-sunos-x86-64-7.0.2/native/libffi bash autogen.sh ./configure cd ../.. bash build.sh # Copy result cp sunos-x86-64.jar ../ cd .. ``` -------------------------------- ### Build JNA for Android Architectures Source: https://github.com/java-native-access/jna/blob/master/www/AndroidDevelopmentEnvironment.md Execute Ant build commands to compile JNA for various Android architectures. Use the '-Dos.prefix' flag to specify the target architecture. ```bash ant -Dos.prefix=android-aarch64 ``` ```bash ant -Dos.prefix=android-armv7 ``` ```bash ant -Dos.prefix=android-arm ``` ```bash ant -Dos.prefix=android-mips64 ``` ```bash ant -Dos.prefix=android-mips ``` ```bash ant -Dos.prefix=android-x86-64 ``` ```bash ant -Dos.prefix=android-x86 ``` -------------------------------- ### Customize dlopen Behavior with Library Options Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md Utilize `Library.OPTION_OPEN_FLAGS` to customize the behavior of `dlopen`. This allows for fine-grained control over how native libraries are loaded. ```java Library.OPTION_OPEN_FLAGS ``` -------------------------------- ### Solaris SPARC 64-bit JNA Native Library Build Source: https://github.com/java-native-access/jna/blob/master/www/BuildingNativeLibraries.md Builds the 64-bit JNA native library for Solaris SPARC by configuring libffi, building, and copying the resulting JAR. ```bash # Reference the 64bit JKD 8 export JAVA_HOME=/opt/csw/java/jdk1.8.0_201 # Build cd build-package-sunos-sparcv9-7.0.2/native/libffi bash autogen.sh ./configure cd ../.. bash build.sh # Copy result cp sunos-sparcv9.jar ../ cd .. ``` -------------------------------- ### Calling COM Methods with COMBindingBaseObject Source: https://github.com/java-native-access/jna/blob/master/www/PlatformLibrary.md Demonstrates how to call COM methods using the COMBindingBaseObject class. This approach requires manual marshalling and unmarshalling of parameters and return values. ```java this.oleMethod(OleAuto.DISPATCH_PROPERTYPUT, result, this.iDispatch, "Visible", new VARIANT(bVisible)); ``` -------------------------------- ### Add Cygwin to PATH Source: https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md Add the Cygwin 64-bit bin directory to the system's PATH environment variable. This is necessary for using Cygwin tools in the command prompt. ```cmd set PATH=C:\cygwin64\bin\;%PATH% ``` -------------------------------- ### Configure libffi with Purify safety checks Source: https://github.com/java-native-access/jna/blob/master/native/libffi/README.md Employ the --enable-purify-safety configure switch to include extra code that suppresses warnings when using Purify. This switch should only be used when Purify is active, as it impacts performance. ```bash ./configure --enable-purify-safety ``` -------------------------------- ### Replace User32#MonitorFromPoint signature Source: https://github.com/java-native-access/jna/blob/master/CHANGES.md Replaces the signature of `com.sun.jna.platform.win32.User32#MonitorFromPoint(Point pt, int dwFlags)` with `com.sun.jna.platform.win32.User32#MonitorFromPoint(Point.ByValue pt, int dwFlags)`. ```java com.sun.jna.platform.win32.User32#MonitorFromPoint(Point pt, int dwFlags) was replaced by com.sun.jna.platform.win32.User32#MonitorFromPoint(Point.ByValue pt, int dwFlags) ```