Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. class StreamGobbler extends Thread
  4. {
  5. InputStream is;
  6. String type;
  7.  
  8. StreamGobbler(InputStream is, String type)
  9. {
  10. this.is = is;
  11. this.type = type;
  12. }
  13.  
  14. public void run()
  15. {
  16. try
  17. {
  18. InputStreamReader isr = new InputStreamReader(is);
  19. BufferedReader br = new BufferedReader(isr);
  20. String line=null;
  21. while ( (line = br.readLine()) != null)
  22. System.out.println(type + ">" + line);
  23. } catch (IOException ioe)
  24. {
  25. ioe.printStackTrace();
  26. }
  27. }
  28. }
  29. public class VagrantLauncher
  30. {
  31. public static void main(String args[])
  32. {
  33. try
  34. {
  35. String[] cmd = new String[3];
  36. cmd[0] = args[0];
  37. cmd[1] = "up";
  38. cmd[2] = "--no-color";
  39. // cmd[3] = "--debug";
  40. Runtime rt = Runtime.getRuntime();
  41. List<String> envp = new ArrayList<>();
  42. for(Map.Entry<String, String> entry : System.getenv().entrySet()) {
  43. envp.add(entry.getKey() + "=" + entry.getValue());
  44. }
  45. System.out.println("Execing " + cmd[0] + " " + cmd[1]
  46. + " " + cmd[2]);
  47. envp.add("SUB_USERNAME=" + args[2]);
  48. envp.add("SUB_PASSWORD=" + args[3]);
  49. Process proc = rt.exec(cmd, envp.toArray(new String[0]), new File(args[1]));
  50. // any error message?
  51. StreamGobbler errorGobbler = new
  52. StreamGobbler(proc.getErrorStream(), "ERROR");
  53.  
  54. // any output?
  55. StreamGobbler outputGobbler = new
  56. StreamGobbler(proc.getInputStream(), "OUTPUT");
  57.  
  58. // kick them off
  59. errorGobbler.start();
  60. outputGobbler.start();
  61.  
  62. // any error???
  63. int exitVal = proc.waitFor();
  64. System.out.println("waitFor returned:" + exitVal);
  65. System.exit(exitVal);
  66. // outputGobbler.join();
  67. // System.out.println("output thread died");
  68. // errorGobbler.join();
  69. // System.out.println("error thread died");
  70. } catch (Throwable t)
  71. {
  72. t.printStackTrace();
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement