Java Tutorial
In java program, any other external application can be opened during program execution.
Runtime class is used to execute the other external application examples like notepad, calculator etc..
Notepad is the text editor available in windows operating system and used to create and edit the text files.
We can open the nodepad from java code using Runtime class.
In java programming,
import java.util.*; import java.io.*; public class PopupAppTest { public static void main(String args[]) { Runtime runtime = Runtime.getRuntime(); try { runtime.exec("notepad"); System.out.println("Notepad is opened!"); } catch(IOException ioe){ System.out.println("Exception: "+ioe); } } }Output:
D:\Java_Programs>javac PopupAppTest.java D:\Java_Programs>java PopupAppTest Notepad is opened!
We can also open the calculator from java code like opening nodepad as below.
import java.util.*; import java.io.*; public class PopupAppTest { public static void main(String args[]) { Runtime runtime = Runtime.getRuntime(); try { runtime.exec("calc"); System.out.println("Calculator is opened!"); } catch(IOException ioe){ System.out.println("Exception: "+ioe); } } }Output:
D:\Java_Programs>javac PopupAppTest.java D:\Java_Programs>java PopupAppTest Calculator is opened!
We can also throw IOException in main method instead of handling the exception using try catch.
import java.util.*; import java.io.*; public class PopupAppTest { public static void main(String args[]) throws IOException { Runtime runtime = Runtime.getRuntime(); runtime.exec("calc"); System.out.println("Calculator is opened!"); } }
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page