Created
February 5, 2014 08:17
-
-
Save digizeph/8819201 to your computer and use it in GitHub Desktop.
Class to call external programs in Java and return the output String.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package util; | |
| import java.io.BufferedReader; | |
| import java.io.InputStreamReader; | |
| /** | |
| * Created by dan on 2/4/14. | |
| */ | |
| public class ProgramCaller { | |
| private String program; | |
| public ProgramCaller(String pname){ | |
| program = pname; | |
| } | |
| public ProgramCaller(){ | |
| program = ""; | |
| } | |
| private String execute(String command){ | |
| StringBuffer output = new StringBuffer(); | |
| Process p; | |
| try { | |
| p = Runtime.getRuntime().exec(command); | |
| p.waitFor(); | |
| BufferedReader reader = | |
| new BufferedReader(new InputStreamReader(p.getInputStream())); | |
| String line = ""; | |
| while ((line = reader.readLine())!= null) { | |
| output.append(line + "\n"); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| return output.toString(); | |
| } | |
| public String runProgram(){ | |
| return execute(program); | |
| } | |
| public String runProgram(String pname){ | |
| return execute(pname); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment