Java examples for Native OS:Shell Command
get Hostname on Linux
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class Main{ static public String getHostname() { return sysExec("/bin/hostname"); }/*from w w w.j a v a 2s . c o m*/ static public String sysExec(String command) { String result = new String(); try { Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec(command); /* * Put a BufferedReader on the command output */ InputStream inputstream = proc.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader( inputstream); BufferedReader bufferedreader = new BufferedReader( inputstreamreader); /* * Read the command output */ String line; while ((line = bufferedreader.readLine()) != null) { result += line; result += "\n"; } /* * Check for command failure */ try { if (proc.waitFor() != 0) { System.err.println("exit value = " + proc.exitValue()); } } catch (InterruptedException e) { System.err.println(e); throw new SystemUtilitiesException(); } } catch (Throwable t) { t.printStackTrace(); } return result; } }