Open In App

Java Runtime load() Method

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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

  • Defines a class called LoadMethod with a main function.
  • Loads the apds.dll library located in the C:/Windows/System32 folder using the Runtime.getRuntime().load(String filename) method.
  • Prints a success message if the library is loaded successfully or an error message if the loading fails.

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




// 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:

  • Loads a custom library named SampleRes.dll located in the C:/Users/Gaurav/Downloads folder.
  • Uses the same approach as before with the Runtime.load() method and a try-catch block.
  • Print messages indicating success or failure based on the loading process.

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




// 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:

  • Tries to load a library named nonexist.dll located in the non-existent path C:/Error
  • loading attempt in a try-catch block to handle any potential errors.
  • Attempts to print a “Library Loaded” message if the library loads successfully (which won’t happen in this case).
  • Catches any UnsatisfiedLinkError that occurs during the loading process.
  • Prints an error message containing the error details.

Exception Handling

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

  • UnsatisfiedLinkError: file not present at the specified directory
  • SecurityException: does not allow loading files for specified directories, if the Security manager suspects any issue
  • NullPointerException: If a NULL file is present in the specified directory

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads