How to open a program like notepad, pdf file using Java?

Java program to open any program or file
Example(s) 
1. Open notepad program file using java.
2. Open a pdf file using java.

This example will open a notepad for you.

1. Simple.java

public class Simple{
 public static void main(String[] args) {
      try {
         System.out.println("Creating Process...");
         Process process = Runtime.getRuntime().exec("notepad.exe");
         process.waitFor();
         // when you manually close the running program notepad.exe, the next line of code will be executed
         System.out.println("Program terminated...");
      } catch (Exception e) {
         System.out.println(":: -----------Exception-------------- :: \n"+e);
      }

   }
}

This is a simple example to open a pdf file using java.

To do so you just need to write some java programming.

Here is the example:

2. PDFOpen.java

import java.io.*;// to get support for creating File class object

public class PDFOpen{
	// main class
	public static void main(String[] args) {
	String fileAddress="D:\\java\\resume.pdf";
		try {
			if ((new File(fileAddress)).exists()) {
				Process process = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler "+fileAddress);
				process.waitFor();
			} else {
				System.out.println("File not found");
			}
			System.out.println("Done");
		} catch (Exception e) {
			System.out.println(":: -----Exception---- ::\n"+e);
		}
	}
}

Note*:

The java.lang.Process.waitFor() method causes the current thread to wait, if necessary until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process. Click to Know more…