Java Utililty Methods exec

List of utility methods to do exec

Description

The list of methods to do exec are organized into topic(s).

Method

StringexecuteShellCommand(String commandName)
This method executes a shell command from Java
String result = "";
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;
String[]executeShellScript(final String shellScript, final File tempDirectory)
execute Shell Script
final File shellFile = new File(tempDirectory, "tempFile_" + UUID.randomUUID() + ".sh");
final String fullPath = getFullPathName(shellFile);
writeString(shellScript, shellFile);
String[] cmd = { "chmod", "777", fullPath };
String[] res = executeCommandLineReturnAll(cmd);
if (res == null || res.length != 0) {
    shellFile.delete();
    return null;
...
voidexecuteSMTPSend(String fromEmailAddress, List toEmailAddresses, String subject, String body, Transport transport, File... fileAttachments)
execute SMTP Send
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmailAddress));
message.setRecipients(RecipientType.TO,
        buildAddresses(toEmailAddresses.toArray(new String[toEmailAddresses.size()])));
message.setSubject(subject);
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(body);
Multipart multiPart = new MimeMultipart();
...
voidexecVmCmdAsync(String vmNameSpace, String vmKey, String vmUser, String vmIp, String vmCmd, String controllerOutPutFile)
exec Vm Cmd Async
booleanexecWindowsCommand(String cmd)
exec Windows Command
if (cmd.contains("echo")) {
    String[] cmdParts = cmd.split(" > ");
    String fileContent = cmdParts[0].substring(5);
    try {
            final java.io.PrintWriter file = new java.io.PrintWriter(cmdParts[1]);
            file.println(fileContent);
            file.close();
...
voidexecWithOutput(File dumpFile, String... args)
exec With Output
StringexecWithResult(String cmd)
exec With Result
return execWithResult(cmd, false);