Example usage for java.lang Runtime exec

List of usage examples for java.lang Runtime exec

Introduction

In this page you can find the example usage for java.lang Runtime exec.

Prototype

public Process exec(String cmdarray[]) throws IOException 

Source Link

Document

Executes the specified command and arguments in a separate process.

Usage

From source file:com.clustercontrol.platform.PlatformPertial.java

public static void setupHostname() {
    String hostname = null;/*from   w  w w  .  jav  a  2  s . c o m*/
    // hinemos.cfg???hostname?
    String etcDir = System.getProperty("hinemos.manager.etc.dir");
    if (etcDir != null) {
        File config = new File(etcDir, "hinemos.cfg");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(config.getAbsolutePath());
            br = new BufferedReader(fr);

            String line;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.trim().startsWith("MANAGER_HOST")) {
                    hostname = line.split("=")[1];
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            log.warn("configuration file not found." + config.getAbsolutePath(), e);
        } catch (IOException e) {
            log.warn("configuration read error." + config.getAbsolutePath(), e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }

            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                }
            }
        }
    }

    if (hostname == null || hostname.length() == 0) {
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        InputStreamReader is = null;
        BufferedReader br = null;

        try {
            process = runtime.exec("hostname");

            is = new InputStreamReader(process.getInputStream());
            br = new BufferedReader(is);
            process.waitFor();

            if (br != null) {
                hostname = br.readLine();
            }

        } catch (IOException | InterruptedException e) {
            log.warn("command execute error.", e);
        } finally {
            if (process != null) {
                process.destroy();
            }

            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }

            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    }

    if (hostname == null) {
        hostname = "";
    }

    System.setProperty("hinemos.manager.hostname", hostname);
}

From source file:org.smartfrog.avalanche.client.sf.apps.utils.FileUtils.java

public static boolean chgPermissions(String fileName, String perms) throws IOException, InterruptedException {
    File file = new File(fileName);
    if (!file.exists()) {
        log.error("File " + fileName + "does not exist");
        return false;
    }// w  ww.j  a  va  2s .  c  o  m

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("chmod " + perms + " " + file.getAbsolutePath());
    int exitVal = 0;
    BufferedReader cmdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    exitVal = p.waitFor();
    if (exitVal != 0) {
        log.error("Error in changing permissions to " + fileName);
        String line = null;
        String error = null;
        if ((line = cmdError.readLine()) != null) {
            log.error(line);
            error = line;
            while ((line = cmdError.readLine()) != null) {
                log.error(line);
                error = error + "\n" + line;
            }
            throw new IOException(error);
        }
    }
    return true;
}

From source file:org.smartfrog.avalanche.client.sf.apps.utils.FileUtils.java

public static boolean chgOwner(String fileName, String userName) throws IOException, InterruptedException {
    File file = new File(fileName);
    if (!file.exists()) {
        log.error("File " + fileName + "does not exist");
        return false;
    }/*from w ww  . j a  v  a2  s . c  om*/

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("chown " + userName + ":" + userName + " " + file.getAbsolutePath());
    int exitVal = 0;
    BufferedReader cmdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    exitVal = p.waitFor();
    if (exitVal != 0) {
        log.error("Error in changing permissions to " + fileName);
        String line = null;
        String error = null;
        if ((line = cmdError.readLine()) != null) {
            log.error(line);
            error = line;
            while ((line = cmdError.readLine()) != null) {
                log.error(line);
                error = error + "\n" + line;
            }
            throw new IOException(error);
        }
    }
    return true;
}

From source file:com.igormaznitsa.sciareto.ui.UiUtils.java

private static void showURLExternal(@Nonnull final URL url) {
    if (Desktop.isDesktopSupported()) {
        final Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(url.toURI());
            } catch (Exception x) {
                LOGGER.error("Can't browse URL in Desktop", x);
            }/*  w ww  .ja va 2 s  . co m*/
        } else if (SystemUtils.IS_OS_LINUX) {
            final Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("xdg-open " + url);
            } catch (IOException e) {
                LOGGER.error("Can't browse URL under Linux", e);
            }
        } else if (SystemUtils.IS_OS_MAC) {
            final Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("open " + url);
            } catch (IOException e) {
                LOGGER.error("Can't browse URL on MAC", e);
            }
        }
    }

}

From source file:org.ensembl.healthcheck.util.StreamGobbler.java

private static Process createShellProcessObject(String command) throws IOException {

    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(new String[] { SHELL, SHELL_FLAGS, command });
    return proc;//from  ww w  .  j  a v  a2  s. c  om
}

From source file:org.ensembl.healthcheck.util.StreamGobbler.java

/**
* Execute the command, capturing output/error into the supplied streams if
* required//from w w  w.j a v a  2 s .  co  m
*
* @param command
* @param out
* @param err
* @return
* @throws Exception
*/
private static int exec(String command, Appendable out, Appendable err, boolean discard) throws IOException {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);

    return waitForProcess(out, err, discard, proc);
}

From source file:org.ensembl.healthcheck.util.StreamGobbler.java

/**
* Execute the command, capturing output/error into the supplied streams if
* required//w  w w .j a  v a  2  s.  c o m
*
* @param commandarray
* @param out
* @param err
* @return
* @throws Exception
*/
private static int exec(String[] commandarray, Appendable out, Appendable err, boolean discard)
        throws IOException {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(commandarray);
    return waitForProcess(out, err, discard, proc);
}

From source file:localization.split.java

public static boolean splitFile(String filepath, String passoloPath, boolean myesri) {
    File log = null;/*ww  w.j a  va 2  s .c  om*/
    try {
        String path = filepath.substring(filepath.lastIndexOf("\\") + 1, filepath.length());
        String folder = filepath.substring(0, filepath.lastIndexOf("\\"));
        String[] files = null;
        Vector<String> zFile;
        if (filepath.endsWith(".zip")) {
            zFile = readzipfile(filepath);
            for (String s : zFile) {
                if (s.endsWith(".lpu")) {
                    File unzipFolder = new File(s.substring(0, s.lastIndexOf("\\")));
                    splitFile(s, passoloPath, myesri);
                }
            }
        } else if (!filepath.endsWith(".lpu")) {
            return false;
        } else {
            if (!checkLPU(filepath, passoloPath)) {
                return false;
            }
            File ECI = new File(folder + "\\ECI");
            File AAC = new File(folder + "\\AAC");
            File TOIN = new File(folder + "\\TOIN");
            File LOIX = new File(folder + "\\LIOX");
            if (!ECI.exists()) {
                ECI.mkdir();
            }
            if (!AAC.exists()) {
                AAC.mkdir();
            }
            if (!TOIN.exists()) {
                TOIN.mkdir();
            }
            if (!LOIX.exists()) {
                LOIX.mkdir();
            }
            if (myesri == true) {
                files = new String[4];
                files[0] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI.lpu";
                files[1] = folder + "\\AAC\\" + path.substring(0, path.lastIndexOf(".")) + "_AAC.lpu";
                files[2] = folder + "\\TOIN\\" + path.substring(0, path.lastIndexOf(".")) + "_TOIN.lpu";
                files[3] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX.lpu";

                File srcDir = new File(filepath);
                File trgDir1 = new File(files[0]);
                File trgDir2 = new File(files[1]);
                File trgDir3 = new File(files[2]);
                File trgDir4 = new File(files[3]);
                try {
                    FileUtils.copyFile(srcDir, trgDir1);
                    FileUtils.copyFile(srcDir, trgDir2);
                    FileUtils.copyFile(srcDir, trgDir3);
                    FileUtils.copyFile(srcDir, trgDir4);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                files = new String[6];
                files[0] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI.lpu";
                files[1] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI_10.lpu";
                files[2] = folder + "\\AAC\\" + path.substring(0, path.lastIndexOf(".")) + "_AAC.lpu";
                files[3] = folder + "\\TOIN\\" + path.substring(0, path.lastIndexOf(".")) + "_TOIN.lpu";
                files[4] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX_10.lpu";
                files[5] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX.lpu";

                File srcDir = new File(filepath);
                File trgDir1 = new File(files[0]);
                File trgDir2 = new File(files[1]);
                File trgDir3 = new File(files[2]);
                File trgDir4 = new File(files[3]);
                File trgDir5 = new File(files[4]);
                File trgDir6 = new File(files[5]);
                try {
                    FileUtils.copyFile(srcDir, trgDir1);
                    FileUtils.copyFile(srcDir, trgDir2);
                    FileUtils.copyFile(srcDir, trgDir3);
                    FileUtils.copyFile(srcDir, trgDir4);
                    FileUtils.copyFile(srcDir, trgDir5);
                    FileUtils.copyFile(srcDir, trgDir6);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            String logfile = folder + "\\" + path.substring(path.lastIndexOf("\\") + 1, path.lastIndexOf("."))
                    + ".log";
            log = new File(logfile);
            if (!log.exists()) {
                log.createNewFile();
            }

            for (int i = 0; i < files.length; i++) {
                int exitVal = 0;
                try {
                    String osName = System.getProperty("os.name");
                    String cmd = "cmd.exe /c " + passoloPath + " /openproject:" + files[i]
                            + " /runmacro=PslLpuSplitter_v3.bas" + " >> " + logfile;
                    System.out.println(cmd);
                    Runtime rt = Runtime.getRuntime();
                    Process proc = rt.exec(cmd);
                    exitVal = proc.waitFor();
                    System.out.println("Exit value: " + exitVal);
                    if (exitVal == 10) {
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                File lpuFile = new File(files[i]);
                File logFile = new File(files[i].substring(0,
                        files[i].substring(0, files[i].lastIndexOf("\\")).lastIndexOf("\\") + 1)
                        + files[i].substring(files[i].lastIndexOf("\\") + 1, files[i].lastIndexOf("."))
                        + ".log");
                if (!lpuFile.exists()) {
                    logFile.delete();
                }
                File lpuFolder = new File(files[i].substring(0, files[i].lastIndexOf("\\")));
                if (lpuFolder.list().length == 0) {
                    lpuFolder.delete();
                }
            } // end for loop  
        }

    } catch (Exception e) {
        try {
            String content = e.getMessage();
            FileWriter fw = new FileWriter(log.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        } catch (Exception e1) {

        }

        return false;
    }
    return true;
}

From source file:fr.inria.oak.paxquery.xparser.client.XClient.java

private static void printDOTFile(String filePath, String dotString) {
    String filePathDot = filePath;
    String filePathPNG = filePath;
    if (filePathDot.startsWith("file://")) {
        filePathDot = filePathDot.substring("file://".length());
        filePathPNG = filePathPNG.substring("file://".length());
    }//w  w w . j  a  v a2  s . c om
    /*
     * int extensionIndex = filePathDot.lastIndexOf("."); if(extensionIndex > -1) { filePathDot =
     * filePathDot.subSequence(0, extensionIndex) + "-pact.dot"; filePathPNG =
     * filePathPNG.subSequence(0, extensionIndex) + "-pact.png"; } else { filePathDot = filePathDot
     * + "-pact.dot"; filePathPNG = filePathPNG + "-pact.png"; }
     */
    if (filePath.endsWith("/") == true) {
        filePathDot = filePathDot + "xoutput-pact.dot";
        filePathPNG = filePathPNG + "xoutput-pact.png";
    } else {
        filePathDot = filePathDot + "/xoutput-pact.dot";
        filePathPNG = filePathPNG + "/xoutput-pact.png";
    }

    try {
        // print the dot file
        FileWriter writer = new FileWriter(filePathDot, false);
        writer.write(dotString);
        writer.close();
        Runtime r = Runtime.getRuntime();
        String com = new String("dot -Tpng " + filePathDot + " -o " + filePathPNG);
        Process p = r.exec(com);
        p.waitFor();
        // System.out.println("PACT plan drawn.");
    } catch (IOException ioe) {
        System.out.println("Error with pact file: " + ioe.getMessage());
    } catch (InterruptedException ie) {
        System.out.println("Error with pact file: " + ie.getMessage());
    }
}

From source file:org.ensembl.healthcheck.util.StreamGobbler.java

/**
 * @param command//w ww  .  j  a  v  a2s  . co  m
 * @param out
 * @param err
 * @param discard
 * @return
 * @throws IOException
 */
private static int execDirect(String command, Appendable out, Appendable err, boolean discard)
        throws IOException {

    // return exit status
    try {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(command);
        // get the exit status of the command once finished
        int exit = proc.waitFor();
        if (!discard) {
            StreamGobbler.streamToBuffer(proc.getInputStream(), out);
            StreamGobbler.streamToBuffer(proc.getErrorStream(), out);
        }
        return exit;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return -1;
    }
}