Java File Attribute shellExecute(String strCommand)

Here you can find the source of shellExecute(String strCommand)

Description

shell Execute

License

Open Source License

Declaration

public static int shellExecute(String strCommand)
    

Method Source Code

//package com.java2s;
/**/*from w  w w .j ava 2 s.co m*/
 * Copyright 2000-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 *
 * This file is part of MARY TTS.
 *
 * MARY TTS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static int shellExecute(String strCommand) {
        return shellExecute(strCommand, true);
    }

    public static int shellExecute(String strCommand,
            boolean bDisplayProgramOutput) {
        Process p = null;
        try {
            p = Runtime.getRuntime().exec(strCommand);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (p != null) {
            BufferedReader input = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = "";
            try {
                line = input.readLine();
                if (bDisplayProgramOutput && line != null)
                    System.out.println(line);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            while (line != null) {
                try {
                    line = input.readLine();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (bDisplayProgramOutput && line != null)
                    System.out.println(line);
            }

            try {
                p.waitFor();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return p.exitValue();
        }

        return -1;
    }
}

Related

  1. setExecute(File f, boolean b)
  2. setExecute(String path)
  3. setExecutePermissions(final File scriptfile)
  4. setRecursiveExecutable(File path)
  5. setSharedPermissions(File file, boolean setExecutable)