Java Runtime load() Method
Last Updated :
28 Dec, 2023
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.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read