Java exec execWindowsCommand(String cmd)

Here you can find the source of execWindowsCommand(String cmd)

Description

exec Windows Command

License

Open Source License

Parameter

Parameter Description
cmd a parameter

Declaration

public static boolean execWindowsCommand(String cmd) 

Method Source Code


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

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Main {
    /**/*ww w .ja  v  a  2s .c om*/
     * 
     * @param cmd
     * @return
     */
    public static boolean execWindowsCommand(String cmd) {
        if (cmd.contains("echo")) {

            String[] cmdParts = cmd.split(" > ");
            // if (CommonConstants.OUTPUT) System.out.println("cmd0 : " + cmdParts[0]);
            // if (CommonConstants.OUTPUT) System.out.println("cmd1 : " + cmdParts[1]);

            String fileContent = cmdParts[0].substring(5);

            try {
                {
                    final java.io.PrintWriter file = new java.io.PrintWriter(cmdParts[1]);
                    file.println(fileContent);
                    file.close();
                }

                return true;
            } catch (Exception err) {
                err.printStackTrace();
                return false;
            }
        } else {
            try {
                String[] execCmd = { cmd };
                String line;
                Process p = Runtime.getRuntime().exec(execCmd);

                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
                input.close();
                return true;
            } catch (Exception err) {
                err.printStackTrace();
                return false;
            }
        }
    }
}

Related

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