• Notepad is a text editor from windows operating system. We use notepad for writing text files.
  • We can open a new notepad using java code.
  • By using the concept of running another application by Runtime class in java.
  • By creating object of runtime and calling  exec() method by passing application name.
  • Lets see how to open a notepad using java code




Program #1: Java example program to open notepad


  1. package interestingJavaprograms;
  2. import java.io.IOException;
  3.  
  4. public class NotepadJava {
  5.  
  6.     /**
  7.      * @ www.instanceofjava.com
  8.      * @ how to open a new notepad using java program
  9.      */
  10. public static void main(String[] args) {
  11.        
  12.           Runtime rt = Runtime.getRuntime();
  13.           
  14. try {
  15.       rt.exec("notepad");
  16. }
  17.  catch (IOException ex) {
  18.  
  19.  System.out.println(ex);
  20.  
  21. }  
  22.  
  23. }
  24.  
  25. }

 Output:

open notepad using java


Program #2: Java example program to open notepad and after 2 seconds close it.


  1. package interestingJavaprograms;
  2. import java.io.IOException;
  3.  
  4. public class NotepadJava {
  5.  
  6.     /**
  7.      * @ www.instanceofjava.com
  8.      * @ how to open a new notepad using java program
  9.      */
  10. public static void main(String[] args) throws InterruptedException, IOException {
  11.         
  12. Runtime runTime = Runtime.getRuntime();
  13. System.out.println("Opening notepad");
  14. Process process = runTime.exec("notepad");
  15.           
  16. try {
  17.  
  18.  
  19. Thread.sleep(200); 

  20.  process.destroy();
  21.  System.out.println("Closing notepad");
  22.  
  23. }
  24.  catch (Exception ex) {
  25.  
  26.  System.out.println(ex);
  27.  
  28. }  
  29.  
  30. }
  31.  
  32. }

  • We can open already existing notepad also for that we need to specify notepad.exe location and path of the destination file.
  • We need to pass these two parameters  to exec method of runtime class.
  • runTime.exec("C:\\Windows\\System32\\notepad.exe E:\\Samplenotepad.txt");

Program #3: Java example program to open exiting notepad txt file.


  1. package interestingJavaprograms;
  2. import java.io.IOException;
  3.  
  4. public class NotepadJava {
  5.  
  6.     /**
  7.      * @ www.instanceofjava.com
  8.      * @ how to open a new notepad using java program
  9.      */
  10. public static void main(String[] args) {
  11.        
  12.           Runtime rt = Runtime.getRuntime();
  13.           
  14. try {
  15.    runTime.exec("C:\\Windows\\System32\\notepad.exe E:\\Samplenotepad.txt");
  16. }
  17.  catch (IOException ex) {
  18.  
  19.  System.out.println(ex);
  20.  
  21. }  
  22.  
  23. }
  24.  
  25. }

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu