Here you can find the source of exec(String cmd)
public static String exec(String cmd)
//package com.java2s; //License from project: Apache License import java.io.InputStreamReader; import java.io.LineNumberReader; public class Main { public static String exec(String cmd) { try {//from www. j ava2s . c o m String[] cmdA = { "/bin/sh", "-c", cmd }; Process process = Runtime.getRuntime().exec(cmdA); LineNumberReader br = new LineNumberReader( new InputStreamReader(process.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = br.readLine()) != null) { System.out.println(line); sb.append(line).append("\n"); } return sb.length() > 0 ? sb.deleteCharAt(sb.length() - 1) .toString() : sb.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }