You are here

Java program to open Notepad

Java program to open Notepad: Notepad, a text editor is installed in Windows operating system. It is used for creating and editing text files. You may be writing Java programs in it but you can also open it using your Java code.

How to open Notepad using Java program?

  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Notepad {
  5.   public static void main(String[] args) {
  6.     Runtime rs = Runtime.getRuntime();
  7.  
  8.     try {
  9.       rs.exec("notepad");
  10.     }
  11.     catch (IOException e) {
  12.       System.out.println(e);
  13.     }
  14.   }
  15. }

Download Notepad program.

Method getRunTime is used to get the reference of current RunTime object, exec method can be used to execute commands. You can also specify a file while opening Notepad such as exec("notepad programming.txt") where 'programming.txt' is the file you wish to open, if the file doesn't exist in the current working directory then a dialog box will be displayed to create a file. You can launch other applications using exec method, for example, exec("calc") will launch calculator application. If an application is present in a directory which isn't set in environment variable PATH, then you can specify complete path of the application. If you are still using Notepad for Java development, it is recommended to switch to some advanced text editor like Notepad++ or use a development IDE.