Here you can find the source of executeShellCommand(String commandName)
Parameter | Description |
---|---|
commandName | String the requested command name with args |
public static String executeShellCommand(String commandName)
//package com.java2s; //License from project: Open Source License import java.io.*; import java.util.HashMap; import java.util.Map; public class Main { /**/* w w w . ja v a 2 s . co m*/ * This method executes a shell command from Java * * @param commandName String the requested command name with args * @return String the output of the command processing */ public static String executeShellCommand(String commandName) { String result = ""; // Put the english locale for the list command Map<String, String> environment = new HashMap<String, String>(System.getenv()); environment.put("LC_ALL", "en_EN"); String[] envp = new String[environment.size()]; int count = 0; for (Map.Entry<String, String> entry : environment.entrySet()) { envp[count++] = entry.getKey() + "=" + entry.getValue(); } try { Process p = Runtime.getRuntime().exec(commandName, envp); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine()) != null) { result += line + "\n"; } return result; } catch (Exception e) { return result; } } }