Java Tutorial
Runtime runtime = Runtime.getRuntime();
public class CallExternalApp { public static void main(String[] args) { try { System.out.println("Opening external app (gedit) in java program"); Runtime runTime = Runtime.getRuntime(); Process process = runTime.exec("gedit"); } catch (Exception ex) { System.out.println("Exception: " + ex); } } }Output:
$ java CallExternalApp Opening external app (gedit) in java program //Opens gedit app
Process exec(String programName) throws IOException
Process exec(String programName, String[] environment) throws IOException
Process exec(String[] commandLineArray) throws IOException
Process exec(String[] commandLineArray, String[] environment) throws IOException
public class CallExternalApp { public static void main(String[] args) { Process process = null; try { System.out.println("Opening external app (gedit) in java program"); Runtime runTime = Runtime.getRuntime(); process = runTime.exec("gedit"); process.waitFor(); } catch (Exception ex) { System.out.println("Exception: " + ex); } System.out.println("gedit returned " + process.exitValue()); } }Output:
$ java CallExternalApp Opening external app (gedit) in java program //Opens gedit app gedit returned 0 //once opened gdit, returns code 0 if no problem is occurred.
import java.io.*; public class CallExternalApp1 { public static void main(String[] args) throws InterruptedException, IOException { String command = "ping -c 5 www.codingpointer.com"; Process pr = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader(new InputStreamReader(pr.getInputStream())); for(String line="";(line = reader.readLine()) != null;) { System.out.print(line + "\n"); } } }Output:
$ java CallExternalApp1 PING codingpointer.com (198.46.136.4) 56(84) bytes of data. 64 bytes from 198.46.136.4 (198.46.136.4): icmp_seq=1 ttl=48 time=248 ms 64 bytes from 198.46.136.4 (198.46.136.4): icmp_seq=2 ttl=48 time=237 ms 64 bytes from 198.46.136.4 (198.46.136.4): icmp_seq=3 ttl=48 time=349 ms 64 bytes from 198.46.136.4 (198.46.136.4): icmp_seq=4 ttl=48 time=261 ms 64 bytes from 198.46.136.4 (198.46.136.4): icmp_seq=5 ttl=48 time=488 ms --- codingpointer.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4004ms rtt min/avg/max/mdev = 237.146/317.018/488.713/94.641 ms
import java.io.*; class CallExternalApp2 { public static void main(String[] args) throws InterruptedException, IOException { Process pr = new ProcessBuilder( "ping","-c","5", "www.google.com").start(); BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream())); for(String line="";(line = br.readLine()) != null;) { System.out.println(line); } } }Output:
$ java CallExternalApp2 PING www.google.com (216.58.220.36) 56(84) bytes of data. 64 bytes from maa03s18-in-f36.1e100.net (216.58.220.36): icmp_seq=1 ttl=56 time=9.64 ms 64 bytes from maa03s18-in-f36.1e100.net (216.58.220.36): icmp_seq=2 ttl=56 time=9.96 ms 64 bytes from maa03s18-in-f36.1e100.net (216.58.220.36): icmp_seq=3 ttl=56 time=9.57 ms 64 bytes from maa03s18-in-f36.1e100.net (216.58.220.36): icmp_seq=4 ttl=56 time=9.94 ms 64 bytes from maa03s18-in-f36.1e100.net (216.58.220.36): icmp_seq=5 ttl=56 time=13.8 ms --- www.google.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4004ms rtt min/avg/max/mdev = 9.576/10.592/13.833/1.629 ms
import java.io.*; class CallExternalApp2 { public static void main(String[] args) throws IOException{ Process pr = new ProcessBuilder( "mkdir","childdir3").start(); } }Output:
$ java CallExternalApp2 //new directory will be created if not exists, otherwise no error is displayed for above program.
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page