How to execute shell command from Java
In Java, you can use the command Runtime.getRuntime().exec to execute shell commands, or any external system command.
p = Runtime.getRuntime().exec("host -t a " + domain); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader( p.getInputStream())); String line = reader.readLine(); while (line != null) { line = reader.readLine(); }
1. IP Address example
See below example, execute shell command host -t a google.com to get all IP addresses that attached to google.com, later uses IP regular expression pattern to display only the IP address.
ExecuteShellComand.java
package com.mkyong; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExecuteShellComand { private static final String IPADDRESS_PATTERN = "([01]?\\d\\d?|2[0-4]\\d|25[0-5])" + "\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])" + "\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])" + "\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])"; private static Pattern pattern = Pattern.compile(IPADDRESS_PATTERN); private static Matcher matcher; public static void main(String[] args) { String domain = "google.com"; Process p; try { p = Runtime.getRuntime().exec("host -t a " + domain); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader( p.getInputStream())); StringBuffer sb = new StringBuffer(); String line = reader.readLine(); sb.append(line); while (line != null) { line = reader.readLine(); sb.append(line); } List<String> list = getIpAddress(sb.toString()); if (list.size() > 0) { System.out.println(domain + " has address : "); for (String ip : list) { System.out.println(ip); } } else { System.out.println(domain + " has NO address."); } } catch (Exception e) { e.printStackTrace(); } } public static List<String> getIpAddress(String msg) { List<String> ipList = new ArrayList<String>(); if (msg == null || msg.equals("")) return ipList; matcher = pattern.matcher(msg); while (matcher.find()) { ipList.add(matcher.group(0)); } return ipList; } }
Output
google.com has address : 74.125.135.139 74.125.135.100 74.125.135.101 74.125.135.102 74.125.135.113 74.125.135.138

You can also use
Please help me…
java.io.IOException: Cannot run program “host”: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at com.levanlong.demo.file.inputstream.IpAddress.main(IpAddress.java:34)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:188)
at java.lang.ProcessImpl.start(ProcessImpl.java:132)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
… 4 more
“host” is a command available in most *nix systems. Make sure your system has installed it.
I have a similar error. I ran it on windows 7 and got same erroe message.
How, sir, can I resolve the problem?
ERROR:
java.io.IOException: Cannot run program “host”: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at javaClasses.ExecuteShellCommand.main(ExecuteShellCommand.java:37)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
… 4 more
BUILD SUCCESSFUL (total time: 2 seconds)
when i’m executing the c++ dll i’m getting the above error. I am unable to think why am i getting this error. the dll consists of code which generates a graphical window. mkyong plz help me in this issue…
Thanks
Raghavendra
Suggestion to try displaying a very simple message box first in dll in order to determine if you gui has the error and not the code. If this works, then the problem is in your graphical window and not the dll or java. Try this:
// buffer256 is declared in header file or cpp file.
static char buffer256[256]; // Generic buffer
// put code below in function in cpp file.
strcpy_s(buffer256, 256, “You are in C++ dll.”);
::MessageBoxA(NULL, buffer256, “Message from MyDll.dll”, 0);
Tip: when working with strings in dll’s, I found that using a char buffer declared in a header file and using it instead of creating and destroying strings in functions eliminated an intermittent bug reported from the Window’s ntdll.dll.