jna | definitive JNA reference ( including an overview and usage | Wrapper library

 by   java-native-access Java Version: 5.7.0-1 License: Non-SPDX

kandi X-RAY | jna Summary

kandi X-RAY | jna Summary

jna is a Java library typically used in Utilities, Wrapper applications. jna has no vulnerabilities and it has high support. However jna has 393 bugs, it build file is not available and it has a Non-SPDX License. You can download it from GitHub, Maven.

The definitive JNA reference (including an overview and usage details) is in the [JavaDoc] Please read the [overview] Questions, comments, or exploratory conversations should begin on the [mailing list] although you may find it easier to find answers to already-solved problems on [StackOverflow] JNA provides Java programs easy access to native shared libraries without writing anything but Java code - no JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python’s ctypes. JNA allows you to call directly into native functions using natural Java method invocation. The Java call looks just like the call does in native code. Most calls require no special handling or configuration; no boilerplate or generated code is required. JNA uses a small JNI library stub to dynamically invoke native code. The developer uses a Java interface to describe functions and structures in the target native library. This makes it quite easy to take advantage of native platform features without incurring the high overhead of configuring and building JNI code for multiple platforms. Read this [more in-depth description] While significant attention has been paid to performance, correctness and ease of use take priority. In addition, JNA includes a platform library with many native functions already mapped as well as a set of utility interfaces that simplify native access.

            kandi-support Support

              jna has a highly active ecosystem.
              It has 7850 star(s) with 1633 fork(s). There are 384 watchers for this library.
              It had no major release in the last 12 months.
              There are 79 open issues and 662 have been closed. On average issues are closed in 22 days. There are 6 open pull requests and 0 closed requests.
              It has a negative sentiment in the developer community.
              The latest version of jna is 5.7.0-1

            kandi-Quality Quality

              jna has 393 bugs (20 blocker, 45 critical, 187 major, 141 minor) and 15778 code smells.

            kandi-Security Security

              jna has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              jna code analysis shows 0 unresolved vulnerabilities.
              There are 41 security hotspots that need review.

            kandi-License License

              jna has a Non-SPDX License.
              Non-SPDX licenses can be open source with a non SPDX compliant license, or non open source licenses, and you need to review them closely before use.

            kandi-Reuse Reuse

              jna releases are not available. You will need to build from source code and install.
              Deployable package is available in Maven.
              jna has no build file. You will be need to create the build yourself to build the component from source.
              Installation instructions are available. Examples and code snippets are not available.
              jna saves you 84840 person hours of effort in developing the same functionality from scratch.
              It has 93243 lines of code, 8083 functions and 627 files.
              It has low code complexity. Code complexity directly impacts maintainability of the code.

            Top functions reviewed by kandi - BETA

            kandi has reviewed jna and discovered the below as its top functions. This is intended to give you an instant insight into jna implemented functionality, and help decide if they suit your requirements.
            • Run the demo .
            • Factory method to create a bubble tip tooltip .
            • Converts a VARIANT to a Java Object .
            • Derives a layout for this VM .
            • Convert the argument at the given index to native .
            • Add listeners to the refresh button .
            • Get a screenshot of the target window .
            • Monitors all the printers .
            • Returns the simple string representation of a given VARType .
            • Invoke a method on a thread .
            Get all kandi verified functions for this library.

            jna Key Features

            No Key Features are available at this moment for jna.

            jna Examples and Code Snippets

            Returns a string identifying the jna class .
            javadot img1Lines of Code : 8dot img1License : Permissive (MIT License)
            copy iconCopy
            public String getUsingNativeClass() {
                    if (com.sun.jna.Native.POINTER_SIZE == 8) {
                        return "64-bit";
                    } else if (com.sun.jna.Native.POINTER_SIZE == 4) {
                        return "32-bit";
                    } else
                        return "unknown"  
            How can I programmatically get the JNA version being used at runtime?
            Javadot img2Lines of Code : 12dot img2License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            import com.sun.jna.Native;
            public class Test {
                public static void main(String[] args) {
                    System.out.println("JNA Version: " + Native.VERSION);
            JNA Version: 5.6.0
            How to capture a Screenshot in native hdpi resolution using awt.Robot?
            Javadot img3Lines of Code : 28dot img3License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            import com.sun.jna.platform.win32.GDI32Util;
            import com.sun.jna.platform.win32.WinDef.HWND;
            import java.awt.image.BufferedImage;
            import java.io.IOException;
            import java.util.logging.Level;
            import java.util.logging.Logger;
            import javax.imag
            Parse input text that is located using left padding spaces
            Lines of Code : 42dot img4License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            $ cat tst.awk
            BEGIN {
                OFS = ","
                print "H1", "H2", "H3", "H4", "H5", "JTT", "JNX", "JNA", "JNO"
            !NF || ($1 == "JTT") { next }
            !/^ / {
                if (NR>1) {
                    print rec
                FS = " "
                $0 = $0
                $1 = $1
                rec = $0
            Split string in assembly
            Lines of Code : 36dot img5License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            ; Assembled on Ubuntu with
            ; wine euroasm.exe split.asm
            ; Linked with
            ; ld split.obj -o split -e Main -m elf_i386
            ; Run with
            ; ./split
            split  PROGRAM Format=COFF, Entry=Main:
                    INCLUDE linapi.htm,cpuext32.htm ; Library
            osgi unresolved requirement: osgi.native
            Lines of Code : 37dot img6License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            /** Shared Library libf403.so */
            private F403 f403;
                /** JNA interface for libf403.so */
            private interface F403 extends
            Counting number of letters in a string
            Lines of Code : 10dot img7License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            mov $0, %ecx
            mov lowerc, %cl
            cmp $len, %ecx
            jna forloop
            cmpb $99, (%esi)   ;Compare with "c"
            je doescountc
            How can you use a mutex between c# and java processes?
            Javadot img8Lines of Code : 31dot img8License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
             * Definitions of the Win32 API. JNA has some for windows, so check first
             * In case CreateMutex is not included.
            public interface Kernel32Ex extends Kernel32 {
                WinNT.HANDLE CreateMutex(

            Community Discussions


            JNA crashes with 64 bit libmx.so on Linux
            Asked 2022-Apr-03 at 09:21

            I have a legacy Java application that uses JNA to interact with Matlab C Matrix API.



            Answered 2022-Apr-03 at 09:21

            The code in this question calls the functions exposed by Matlab Compiler Runtime via JNA. It turns out that the interface has been changed on Matlab side for 64bit architecture. They use mwSize type in their native API signature while our JNA mapping uses int. On a 64-bit machine where mwSize is equivalent to size_t, an int will be 32-bits. Therefore, the JNA mapping and the native API signature do not match anymore. After repeatedly calling these functions via JNA, it even causes JNA to crash.

            The solution direction is to replace int with a 64-bit integer on Java code.This can be done by extending the abstract class called IntegerType provided by JNA and using this extended class wherever applicable

            Reference article from Matlab: https://nl.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html

            Source https://stackoverflow.com/questions/71712247


            Java, Intellij IDEA problem Unrecognized option: --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
            Asked 2022-Mar-26 at 15:23

            I have newly installed



            Answered 2021-Jul-28 at 07:22

            You are running the project via Java 1.8 and add the --add-opens option to the runner. However Java 1.8 does not support it.

            So, the first option is to use Java 11 to run the project, as Java 11 can recognize this VM option.

            Another solution is to find a place where --add-opens is added and remove it. Check Run configuration in IntelliJ IDEA (VM options field) and Maven/Gradle configuration files for argLine (Maven) and jvmArgs (Gradle)

            Source https://stackoverflow.com/questions/68554693


            Building Docker image from spring maven project for arm64 platform
            Asked 2022-Mar-24 at 00:10

            I'm in a Spring course, and I have a maven application (downloaded from the course resources) built from spring initializr. I can build a local Docker image with mvn spring-boot:build-image (no Dockerfile in the project). By default a Docker image is built as linux/amd64, but I am working with a M1 Apple Silicon chip (arm64). I've been looking many workarounds but with no success. Lastly, I found that maybe adding a Dockerfile and specifying the platform it would build the image accordingly.

            My goal is to build a docker image for arm64 architecture.

            So, I created a Dockerfile:



            Answered 2022-Mar-24 at 00:10

            Building an ARM-based image is not currently possible with mvn spring-boot:build-image, because the Cloud Native Buildpacks builders that Spring Boot integrates with do not support this. This is one of the possible items of focus on the Paketo buildpacks 2022 roadmap, which you can cast votes for.

            CNB documents a work-around for this, but it's not simple to set up and run.

            RUN mvn -f /home/path_to_app/pom.xml spring-boot:build-image -DskipTests

            You would need Docker-in-Docker to make something like this work, since the CNB builder processes that would run inside the Docker container need to talk to the Docker daemon. Regardless, this would not allow you to build an ARM image for the reasons stated above.

            Source https://stackoverflow.com/questions/71581008


            java.lang.UnsatisfiedLinkError when starting the play project
            Asked 2022-Feb-25 at 04:58

            Im trying to start a project on play in IntelliJ IDEA Ultimate MacBook Pro on M1, I get the following error in the console:

            [error] java.lang.UnsatisfiedLinkError: /Users/username/Library/Caches/JNA/temp/jna2878211531869408345.tmp: dlopen(/Users/username/Library/Caches/JNA/temp/jna2878211531869408345.tmp, 0x0001): tried: '/Users/username/Library/Caches/JNA/temp/jna2878211531869408345.tmp' (fat file, but missing compatible architecture (have 'i386,x86_64', need 'arm64e')), '/usr/lib/jna2878211531869408345.tmp' (no such file)

            I tried to reinstall the JDK on the arm architecture after deleting all the JDKs, it did not help

            What needs to be tricked to fix this?

            Full StackTrace:



            Answered 2022-Feb-25 at 04:58

            Found a solution: Inside sbt 1.4.6 there is a JNA library version 5.5.0, which apparently does not have the necessary files for the arm64 architecture processor Raising the sbt version to 1.6.2 helped

            Source https://stackoverflow.com/questions/71252965


            Java Native Access incorrect Windows Handle
            Asked 2022-Feb-15 at 18:53

            I'm using javafx to make an overlay app. I want to send key input to a third party application when a button is pressed. I'm using JNA to focus the application and Robot to simulate key pressing.

            I can correctly get a Windows Handle with FindWindows:



            Answered 2022-Feb-14 at 16:36

            You have an invalid handle, just not the one you think.

            The problem is in your function mapping for SetWindowPos:

            Source https://stackoverflow.com/questions/71108819


            Invalid memory access in JNA
            Asked 2022-Jan-31 at 19:30

            When using JNA, I am receiving an Invalid Memory Access error when invoking the QLConnect method.

            Here is my interface mapping the DLL:



            Answered 2022-Jan-31 at 19:05

            The first thing to check when you get Invalid Memory Access errors is your type mapping, which you correctly started with looking at the int return value. Turns out the String mapping for the other arguments, which works fine on *nix systems, needs a little special treatment on Windows.

            By default Windows uses UTF 16 encoding, and JNA's default type mapping under-the-hood tries to convert Java String objects to UTF-16 encoded native strings (wchar*). This works fine for Windows API DLLs.

            However, your custom DLL specified that the User Name and Password arguments should be in ASCIIZ format (null-terminated C strings).

            You can force JNA to use an ASCII String type mapper by including W32APIOptions.ASCII_OPTIONS as a third argument when loading the DLL, e.g.,

            Source https://stackoverflow.com/questions/70899331


            MacOS link library containing CoreBluetooth framework
            Asked 2022-Jan-14 at 06:08

            I'm attempting to use Java Native Access (JNA) to provide a Java interface to the CoreBluetooth framework that's an integral part MacOS (version 12.1, in my case).

            It appears that I need access to a MacOS link library in order for JNA to determine the elements of the interface that are available. However, I can't seem to locate the link library file that provides the classes, methods, etc. for this framework.

            I've looked inside the following bundles with no success:

            • /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreBluetooth.framework
            • /System/Library/Frameworks/CoreBluetooth.framework
            • /System/Library/PrivateFrameworks/CoreBluetoothUI.framework

            I've scanned the contents of /usr/lib too, but I'm not seeing anything related to CoreBluetooth, and I don't want to have to look at the contents of every .dylib file on the system.

            Any suggestions where I can find this file? Or are there any other ways I can setup a JNA interface to CoreBluetooth?



            Answered 2022-Jan-14 at 06:08

            You can just load the library by name in JNA as usual (version 5.6 or greater).

            Source https://stackoverflow.com/questions/70690580


            JNA Callback crashing the application intermittently on specific internal method call to native library
            Asked 2022-Jan-11 at 04:50

            In my application, I am using JNA to use the native code written in C. Where I get the notification from native application in callback.

            In callback, I get a pointer and some other data to process. In JNA callback code, I have to use this pointer again to call some other native library code and have to pass that pointer. After that I have to return from this callback.

            If I don't call that intermediate native library method from callback, which passes the pointer, it works fine, but if I add this call my application crashes intermittently (mostly after processing few hundred of callback requests, sometimes it run for thousands of callbacks also sucessfully).

            This NotificationHook class objects for which hook is set up in native code is a static variable, as there will be only one hook for the application. And native library call this one by one.



            Answered 2022-Jan-10 at 21:50

            Based on the code you've provided, the problem is the same as other Callback-related questions here: you're losing the native allocation of TRANX due to Java's garbage collection.

            A JNA Structure consists of two parts: a pointer (to data), and the data itself. You have not provided the native typedef for TRANX to confirm your JNA mapping, but an instantiated object will have an internal pointer reference, pointing to a 4-byte allocation of memory (the int unused).

            You only show the callback code where TRANX is already an argument, meaning you've already instantiated it to pass to the callback.

            If you allocated it yourself using new TRANX() or new TRANX(int unused), then JNA has

            • allocated 4 bytes of native memory
            • stored the pointer to it internally

            In JNA, the native memory attached to a Structure is automatically freed as a part of the garbage collection process. This is a common problem with callbacks, as you generally don't control the timing of the callback return, so the following sequence occurs:

            • You create the object in Java (allocating the native 4 bytes which the TRANX structure tracks the pointer to internally)
            • You pass the TRANX object to the callback
            • Immediately after passing the object, Java no longer has need for its own object; it is unreachable and thus eligible for garbage collection
            • When GC occurs the native 4 bytes are freed as part of the process
            • The TRANX object in the callback still has the pointer internally, but it now points to memory that is no longer allocated, resulting in the SIGSEGV (or Invalid Memory Access error, or strange symptoms if the memory is allocated by another thread, or other undefined behavior).

            The solution to the problem is to track the memory associated with TRANX.

            • If you are allocating it yourself, keep a reference to the TRANX object to prevent it from being unreachable.
              • This generally requires accessing the TRANX structure at some later point after you are sure the callback will have been processed
              • In JDK9+ a ReachabilityFence can be used for this.
              • In JDK8 you should manipulate the class in some way (e.g., read a value from it, or call toString on it, etc.).
            • If you are using a native allocation and creating the pointer from the peer value returned from the native API, then read the API to determine when that memory is freed.

            Source https://stackoverflow.com/questions/70655937


            How to write to windows eventlog using Java and JNA
            Asked 2022-Jan-07 at 16:27

            I was looking at a way to write to windows event log using JNA. I am able to write to windows event log using log4J2 and Log4JNA libraries.

            However, I would like to write directly using JNA and I am not comfortable having to add a dll file, which is required by Log4JNA.

            I am currently Looking at Advapi32 and Advapi32Util but couldn't find any methods to write to event log.

            How can this be done?



            Answered 2022-Jan-07 at 16:26

            The WINAPI call you need is ReportEvent.

            This is mapped in the user-contributed platform mappings in JNA in Advapi32.

            The Advapi32Test class contains code demonstrating writing an event. I've excerpted portions of this test code below:

            Source https://stackoverflow.com/questions/70622738


            Launching chrome in headless mode with selenium in Java giving error
            Asked 2021-Dec-31 at 13:08

            This is my code:



            Answered 2021-Dec-31 at 13:08

            You need to take care of a few things here:

            • You have already download the ChromeDriver and accessing it as:

            Source https://stackoverflow.com/questions/70538475

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network


            No vulnerabilities reported

            Install jna

            You can download it from GitHub, Maven.
            You can use jna like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the jna component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .


            JNA will build on most linux-like platforms with a reasonable set of GNU tools and a JDK. See the native [Makefile](https://raw.githubusercontent.com/java-native-access/jna/master/native/Makefile) for native configurations that have been built and tested. If your platform is supported by [libffi](http://en.wikipedia.org/wiki/Libffi), then chances are you can build JNA for it. Pre-built platform support may be found [here](https://github.com/java-native-access/jna/tree/master/lib/native).
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
          • HTTPS


          • CLI

            gh repo clone java-native-access/jna

          • sshUrl


          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link

            Explore Related Topics

            Consider Popular Wrapper Libraries


            by java-native-access


            by serialport


            by eddiezane


            by KeepSafe


            by pyserial