Runtime loadLibrary(String) in Java

loadLibrary(String): This method is available in the java.lang.Runtime class of Java.

Syntax:

void java.lang.Runtime.loadLibrary(String libname)

This method takes one argument. This method loads the native library specified by the libname argument.

The libname argument must not contain any platform-specific prefix, file extension, or path. If a native library called libname is statically linked with the VM, then the NI_OnLoad_libname function exported by the library is invoked.

Parameters: One parameter is required for this method.

libname: the name of the library.

Throws:

1. SecurityException - if a security manager exists and its checkLink method doesn't allow loading of the specified dynamic library.

2. UnsatisfiedLinkError - if either the libname argument contains a file path, the native library is not statically linked with the VM, or the library cannot be mapped to a native library image by the host system.

3. NullPointerException - if libname is null.

Approach 1: When no exception

Java

package com.Runtime;

public class RuntimeloadLibrary {
    public static void main(String[] args) {

        Runtime runtime = Runtime.getRuntime();

        String libname = "C:\\Windows\\System32\\AdvancedInstallers\\cmiv2.dll";
        runtime.loadLibrary(libname);
        System.out.println("Successfully loadLibrary");

    }
}

Output:

Successfully loadLibrary


Approach 2: UnsatisfiedLinkError

Java

package com.Runtime;

public class RuntimeloadLibrary {
    public static void main(String[] args) {

        Runtime runtime = Runtime.getRuntime();

        String libname = "C:\\Windows\\System32\\hello.dll";
        runtime.loadLibrary(libname);
        System.out.println("Successfully loadLibrary");

    }
}

Output:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Directory separator should not appear in library name: C:\Windows\System32\hello.dll at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:806) at java.base/java.lang.Runtime.loadLibrary(Runtime.java:797) at com.Runtime.RuntimeloadLibrary.main(RuntimeloadLibrary.java:9)


Approach 3: NullPointerException 

Java

package com.Runtime;

public class RuntimeloadLibrary {
    public static void main(String[] args) {

        Runtime runtime = Runtime.getRuntime();

        String libname = null;
        runtime.loadLibrary(libname);
        System.out.println("Successfully loadLibrary");

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "libname" is null at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:805) at java.base/java.lang.Runtime.loadLibrary(Runtime.java:797) at com.Runtime.RuntimeloadLibrary.main(RuntimeloadLibrary.java:9)


Some other methods of Runtime

addShutdownHook(Thread)This method registers a new virtual machine shutdown hook.

availableProcessors()This method returns the number of processors available to the Java virtual machine.

exec(String)This method executes the specified string command in a separate process.

exec(String[])This method executes the specified command and arguments in a separate process.

exec(String, String[])This method executes the specified string command in a separate process with the specified environment.

exec(String[], String[])This method executes the specified command and arguments in a separate process with the specified environment.

exec(String, String[], File)This method executes the specified string command in a separate process with the specified environment and working directory.

exec(String[], String[], File)This method executes the specified command and arguments in a separate process with the specified environment and working directory.

gc()This method runs the garbage collector in the Java Virtual Machine.

Runtime.getRuntime()This method returns the runtime object associated with the current Java application.

halt(int)This method forcibly terminates the currently running Java virtual machine.

load(String)This method loads the native library specified by the filename argument.

loadLibrary(String)This method loads the native library specified by the libname argument.

maxMemory()This method returns the maximum amount of memory that the Java virtual machine will attempt to use.

removeShutdownHook(Thread)This method De-registers a previously registered virtual machine shutdown hook.

runFinalization()This method runs the finalization methods of any objects pending finalization.

version()This method returns the version of the Java Runtime Environment as a Version.

exit(int)This method terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally.

freeMemory()This method returns the amount of free memory in the Java Virtual Machine.

totalMemory()This method returns the total amount of memory in the Java virtual machine.

No comments:

Post a Comment