Open In App

Java Runtime load() Method

In Java Programming Langauge the Runtime class mainly allows us to interact with the runtime environment of our application. There is one of the important method load() provided by this Runtime class which allows Java applications to dynamically load the libraries into the Java environment. In this article, we will see the detailed overview, syntax, and example of Java Runtime load() Method.

Java Runtime load() Method

The Java Runtime load() Method in the Runtime class is mainly used to load the specified filename as a dynamic library into our Java environment application. This method only accepts the library name in terms of complete path names as input and allows that tile to be loaded into the application. There is no return type value for this function as it is operated as a void function.



Syntax of load() Method

public void load(String filename)

Examples of Java Runtime load() Method

Examples of using the runtime objects are mentioned below:

Example 1:

Using the Java Runtime method load() we will try to load a system file.



Below is a demonstration of how to Load a Library:




// Java Program to implement
// Java Runtime load()
public class LoadMethod {
  
    // main function
    public static void main(String[] args)
    {
  
        // Example 1: Load a library from the
        // Windows/System32 folder
        try {
  
            // Loading System32 dll file
            Runtime.getRuntime().load(
                "C:/Windows/System32/apds.dll");
            System.out.println("Library Loaded.");
        }
        catch (UnsatisfiedLinkError e) {
            System.out.println(
                "Failed to load library. Error: "
                + e.getMessage());
        }
    }
}

Output:

Library Loaded.

Explanation of the above Program

Example 2:

Using the Java Runtime method load() we will try to load a file present at a custom path location.

Below is the implementation of how to Load a file from a Custom location:




// Java Program to implement
// Java Runtime load()
public class LoadMethod {
  
    // main function
    public static void main(String[] args)
    {
  
        // Example 2: Load a library from a custom location
        try {
  
            // Loading custom dll file
            Runtime.getRuntime().load(
                "C:/Users/Gaurav/Downloads/SampleRes.dll");
            System.out.println("Library Loaded.");
        }
        catch (UnsatisfiedLinkError e) {
            System.out.println(
                "Failed to load library. Error: "
                + e.getMessage());
        }
    }
}

Output:

Library Loaded.

Explanation of the above Program:

Example 3:

Using the Java Runtime method load() we will try to load a file present at a non-existing location

Below is the implementation of loading a non-existent library:




// Java Program to implement
// Java Runtime load()
public class LoadMethod {
  
    // main function
    public static void main(String[] args)
    {
  
        // Example 3: Attempt to load a non-existent library
        try {
  
            // Loading non exist file
            Runtime.getRuntime().load(
                "C:/Error/nonexist.dll");
            System.out.println("Library Loaded.");
        }
        catch (UnsatisfiedLinkError e) {
            System.out.println(
                "Failed to load library. Error: "
                + e.getMessage());
        }
    }
}

Output:

Failed to load library. Error: Can't load library: C:/Error/nonexist.dll

Explanation of the above Program:

Exception Handling

To handle exceptions for the load() method of Java Runtime, we have these:

Conclusion

The Runtime.load() method provides the process for dynamically loading the native libraries into the Java application. It allows the proper loading of existing files and also provides the Exception when a non-existent file is attempted to be loaded in the application. This method is efficient integration of libraries into our native application code.


Article Tags :