Java native util java native library

Java Language Java Native Interface Loading native libraries

The common idiom for loading shared library files in Java is the following :

public class ClassWithNativeMethods < static < System.loadLibrary("Example"); >public native void someNativeMethod(String arg); . 

Calls to System.loadLibrary are almost always static so as to occur during class loading, ensuring that no native method can execute before the shared library has been loaded. However the following is possible :

public class ClassWithNativeMethods < // Call this before using any native method public static void prepareNativeMethods() < System.loadLibrary("Example"); >. 

This allows to defer shared library loading until necessary, but requires extra care to avoid java.lang.UnsatisfiedLinkError s.

Target file lookup

Shared library files are searched for in the paths defined by the java.library.path system property, which can be overriden using the -Djava.library.path= JVM argument at runtime :

java -Djava.library.path=path/to/lib/:path/to/other/lib MainClassWithNativeMethods 

Watch out for system path separators : for example, Windows uses ; instead of : .

Note that System.loadLibrary resolves library filenames in a platform-dependent manner : the code snippet above expects a file named libExample.so on Linux, and Example.dll on Windows.

An alternative to System.loadLibrary is System.load(String) , which takes the full path to a shared library file, circumventing the java.library.path lookup :

public class ClassWithNativeMethods < static < System.load("/path/to/lib/libExample.so"); >. 

pdf

PDF — Download Java Language for free

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A simple library class which helps with loading dynamic JNI libraries stored in the JAR archive

License

adamheinrich/native-utils

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

The tested method throws IllegalArgumentException instead of NullPointerException when called with a null argument.

Git stats

Files

Failed to load latest commit information.

README.md

A simple library class which helps with loading dynamic libraries stored in the JAR archive. These libraries usualy contain implementation of some methods in native code (using JNI — Java Native Interface).

  • The temporary file is stored into temp directory specified by java.io.tmpdir (by default it’s the operating system’s temporary directory). It should be automatically deleted when the application exits.
  • Although the code has some try-finally section (to be sure that streams are closed properly in case an exception is thrown), it does not catch exceptions. The exception has to be handled by the application. I belive this approach is cleaner and has some benefits.

To load the dynamic library, just make sure it is packed inside the JAR archive and call method loadLibraryFromJar:

import cz.adamh.NativeUtils; public class HelloJNI < static < try < NativeUtils.loadLibraryFromJar("/resources/libHelloJNI.so"); >catch (IOException e) < // This is probably not the best way to handle exception :-) e.printStackTrace(); >> public native void hello(); > 

More information can be found in accompanying blog post.

About

A simple library class which helps with loading dynamic JNI libraries stored in the JAR archive

Источник

Читайте также:  Events for html select
Оцените статью