import java.util.*;
import java.io.*;
class Notepad
{
public static void main(String[] args)
{
Runtime rs = Runtime.getRuntime();
try
{
rs.exec("notepad");
}
catch (IOException e)
{
System.out.println(e);
}
}
}
Program Description:
This is a Java program that invokes the Notepad application in Windows by using the Runtime.exec() method. The exec() method lets Java programs invoke system-level commands or programs.
Step-by-Step Explanation
1. Importing the Classes Needed
import java.util.*;
import java.io.*;
- java.util.*: No class in this program uses java.util.
- java.io.*: This import is required as the program calls the IOException, which is declared in the package java.io.
2. Class Declaration:
class Notepad {
Here, the class Notepad is declared which contains the main method. The main method is the entry point to the Java application.
3. Main Method:
public static void main(String[] args) {
The primary method is where the program execution starts. It is the entry point of the program.
4. Runtime Object Creation:
Runtime rs = Runtime.getRuntime();
Runtime.getRuntime() returns the runtime object for the current Java application. It can access the environment in which the Java application is running-the operating system.
rs is the variable indicating the Runtime object, which offers methods for executing system commands.
5. Execution of the Notepad Program:
rs.exec(“notepad”);
This statement calls the exec() method to execute the notepad command.
- On Windows operating systems, issuing the notepad command opens Notepad. The exec() method will run system commands as if one had typed them in at the command line.
- It means that Notepad application is called from within the program in Java.
6. Exception Handling:
catch (IOException e) { System.out.println(e); }
- Here, the try-catch block is used to catch any possible exceptions that may be encountered while executing the exec() method.
- If the exec() method fails (for example, if the system does not recognize the command), it will throw an IOException, which will be caught and printed by the catch block.
- The exception message is printed with System.out.println(e);.
7. Closing of the Class and Main Method:
The closing curly brace also closes the main method, as well as the Notepad class.
What Happens When the Program Executes?
- If you run this program, then the exec() method invokes the system command notepad, which just launches the Notepad application in a Windows OS.
- If the exec() method fails; for instance, if it’s not a Windows system or there is an issue opening Notepad, an IOException is thrown and the message of this exception is printed to the console.
Output:
1. If Notepad is opened successfully
- No output to console (unless an exception)
- Notepad as a separate window outside the Java program
2. There is an exception thrown (IOException):
- If the system cannot recognize the notepad command or otherwise fail to launch Notepad, the exception message will print to the console, such as the following:
- java.io.IOException: Cannot run program “notepad”: CreateProcess error=2, The system cannot find the file specified.
- This is a statement which says the exec() method cannot find the program, namely notepad, though it may be running on an unsupported system, or the path to Notepad is not set correctly.
Important Points
- Runtime.getRuntime().exec(“notepad”) Runs an external program in Java. In the above statement, it opens the Notepad application.
- IOException The system is unable to execute the given command; this includes not being able to find the application or if it has some sort of permissions issue.
- try-catch block: The exception is caught and treated nicely so that the program does not crash in case of an error while executing the system command.
Conclusion:
- It is shown how Java programs can interact with the operating system to run an application. In this case, how it could launch Notepad using Runtime.exec().
- IOException is caught to avoid a possible crash of the program if something went wrong in launching Notepad.