Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. String file = "script.sh";
  2. File shellScriptFile= new File(file);
  3. Runtime run = Runtime.getRuntime();
  4. ProcessBuilder processBuilder = new ProcessBuilder(shellScriptFile.getAbsolutePath(), "Argument-ONE" , "Argument-TWO");
  5. Process process = processBuilder.start();
  6.  
  7. Process process = processBuilder.start();
  8. int exitCode = process.waitFor();
  9. switch(exitCode) {
  10. case 0:
  11. // everything is ok
  12. break;
  13. case 1:
  14. // handle
  15. break;
  16. // and so on ...
  17. default:
  18. // default
  19. }
  20.  
  21. package testShellScript;
  22.  
  23. import java.io.IOException;
  24. import org.apache.commons.exec.CommandLine;
  25. import org.apache.commons.exec.DefaultExecutor;
  26. import org.apache.commons.exec.ExecuteException;
  27.  
  28. public class TestScript {
  29. int iExitValue;
  30. String sCommandString;
  31.  
  32. public void runScript(String command){
  33. sCommandString = command;
  34. CommandLine oCmdLine = CommandLine.parse(sCommandString);
  35. DefaultExecutor oDefaultExecutor = new DefaultExecutor();
  36. oDefaultExecutor.setExitValue(0);
  37. try {
  38. iExitValue = oDefaultExecutor.execute(oCmdLine);
  39. } catch (ExecuteException e) {
  40. // TODO Auto-generated catch block
  41. System.err.println("Execution failed.");
  42. e.printStackTrace();
  43. } catch (IOException e) {
  44. // TODO Auto-generated catch block
  45. System.err.println("permission denied.");
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. public static void main(String args[]){
  51. TestScript testScript = new TestScript();
  52. testScript.runScript("sh /root/Desktop/testScript.sh");
  53. }
  54. }
  55.  
  56. public static void main(String[] args) {
  57. String[] cmd = { "/bin/sh", "-c", "script.sh" };
  58. BufferedReader bri = null, bre = null;
  59. int exitC = 0;
  60. try {
  61. Process p = Runtime.getRuntime().exec(cmd);
  62. exitC = p.waitFor();
  63. bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
  64. bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  65. String line = "";
  66. while ((line = bri.readLine()) != null) {
  67. System.out.println(line);
  68. }
  69. while ((line = bre.readLine()) != null) {
  70. System.out.println(line);
  71. }
  72. bri.close();
  73. bre.close();
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. System.out.println("Exit Code: "+ exitC);
  78. }
  79.  
  80. String file = "script.sh";
  81. File shellScriptFile= new File(file);
  82. Runtime run = Runtime.getRuntime();
  83. ProcessBuilder processBuilder = new ProcessBuilder(shellScriptFile.getAbsolutePath(), "Argument-ONE" , "Argument-TWO");
  84. try {
  85. Process process = processBuilder.start();
  86. int exitC = process.waitFor();
  87. BufferedReader bri = new BufferedReader(new InputStreamReader(process.getInputStream()));
  88. String inputLine = "";
  89. while ((inputLine = bri.readLine()) != null) {
  90. System.out.println(inputLine);
  91. }
  92. BufferedReader bre = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  93. String errorLine = "";
  94. while ((errorLine = bre.readLine()) != null) {
  95. System.out.println(errorLine);
  96. }
  97. System.out.println("Exit Code:" + exitC);
  98. } catch (IOException e){
  99. //captured Exception Here
  100. //e.printStackTrace();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement