Here you can find the source of executeCommandLinux(final String _command)
Parameter | Description |
---|---|
_command | the command that will be executed. |
public static String executeCommandLinux(final String _command)
//package com.java2s; /*//ww w .ja v a 2 s.co m * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { /** * Constants that indicate whether an execution (of a command inside the * terminal) was successful or not. */ public static final String EXECUTION_SUCCESS = "Success", EXECUTION_FAILED = "Failure"; /** * Execute linux command in terminal and return the result. * * @param _command the command that will be executed. * @return the answer of the linux command */ public static String executeCommandLinux(final String _command) { Process p; String answer = "", s; try { //execute command p = Runtime.getRuntime().exec(_command); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); //fetch answer while ((s = br.readLine()) != null) { answer += s; } p.waitFor(); //if normal termination if (p.exitValue() == 0) { answer = EXECUTION_SUCCESS + ": " + answer; } else { answer = EXECUTION_FAILED + ": " + answer; } //destroy execution process and return the result p.destroy(); return answer; } catch (Exception e) { //print stack trace and return the failure message. // e.printStackTrace(); return EXECUTION_FAILED; } } }