Java exec executeShellScript(final String shellScript, final File tempDirectory)

Here you can find the source of executeShellScript(final String shellScript, final File tempDirectory)

Description

execute Shell Script

License

Open Source License

Declaration

public static String[] executeShellScript(final String shellScript,
            final File tempDirectory) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.UUID;

import java.io.IOException;
import java.util.ArrayList;

import java.io.InputStreamReader;

import java.io.BufferedReader;
import java.io.File;

public class Main {
    public static String[] executeShellScript(final String shellScript,
            final File tempDirectory) {
        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();//from   w  w  w.j a  va2s . c o m
            return null;
        }
        cmd = new String[] { fullPath };
        res = executeCommandLineReturnAll(cmd);
        shellFile.delete();
        return res;
    }

    public static String getFullPathName(final File fileDirectory) {
        String full = null;
        try {
            full = fileDirectory.getCanonicalPath();
        } catch (IOException e) {
            System.out.println("Problem with getFullPathtName(), "
                    + fileDirectory);
            e.printStackTrace();
        }
        return full;
    }

    public static boolean writeString(final String data, final File file) {
        try {
            final PrintWriter out = new PrintWriter(new FileWriter(file));
            out.print(data);
            out.close();
            return true;
        } catch (IOException e) {
            System.out.println("Problem writing String to disk!");
            e.printStackTrace();
            return false;
        }
    }

    public static String[] executeCommandLineReturnAll(
            final String[] command) {
        final ArrayList<String> al = new ArrayList<String>();
        try {
            final Runtime rt = Runtime.getRuntime();
            rt.traceInstructions(true);
            rt.traceMethodCalls(true);
            final Process p = rt.exec(command);
            final BufferedReader data = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            final BufferedReader error = new BufferedReader(
                    new InputStreamReader(p.getErrorStream()));
            String line;
            while ((line = data.readLine()) != null) {
                al.add(line);
            }
            while ((line = error.readLine()) != null) {
                al.add(line);
            }
            data.close();
            error.close();
        } catch (Exception e) {
            System.out.println("Problem executing -> "
                    + stringArrayToString(command, " ") + " "
                    + e.getLocalizedMessage());
            e.printStackTrace();
            return null;
        }
        final String[] res = new String[al.size()];
        al.toArray(res);
        return res;
    }

    public static String stringArrayToString(final String[] s,
            final String separator) {
        if (s == null) {
            return "";
        }
        final int len = s.length;
        if (len == 1) {
            return s[0];
        }
        if (len == 0) {
            return "";
        }
        final StringBuffer sb = new StringBuffer(s[0]);
        for (int i = 1; i < len; ++i) {
            sb.append(separator);
            sb.append(s[i]);
        }
        return sb.toString();
    }
}

Related

  1. executeShellCmdAndReturn(String cmd)
  2. executeShellCommand(String command)
  3. executeShellCommand(String command)
  4. executeShellCommand(String command, File dir)
  5. executeShellCommand(String commandName)
  6. executeSMTPSend(String fromEmailAddress, List toEmailAddresses, String subject, String body, Transport transport, File... fileAttachments)
  7. execVmCmdAsync(String vmNameSpace, String vmKey, String vmUser, String vmIp, String vmCmd, String controllerOutPutFile)
  8. execWindowsCommand(String cmd)
  9. execWithOutput(File dumpFile, String... args)