cmd.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.fd.test;
  2. import java.io.*;
  3. /**
  4. * Created by Owen on 2019/10/31 0031 14:49
  5. */
  6. public class cmd {
  7. public static void main(String[] args) {
  8. // String result = exeCmd();
  9. exeCmdMulti_1();
  10. }
  11. public static String exeCmd() {
  12. String command = "ifconfig";
  13. String result = "";
  14. try {
  15. String[] cmd = new String[]{"/bin/sh", "-c", command};
  16. Process ps = Runtime.getRuntime().exec(cmd);
  17. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  18. StringBuffer sb = new StringBuffer();
  19. String line;
  20. while ((line = br.readLine()) != null) {
  21. sb.append(line).append("\n");
  22. }
  23. result = sb.toString();
  24. System.out.println("result: " + result);
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. return result;
  29. }
  30. public static String exeCmdMulti() {
  31. String result = "";
  32. String command = "ifconfig;ls";
  33. try {
  34. String[] cmd = new String[]{"/bin/sh", "-c", command};
  35. Process ps = Runtime.getRuntime().exec(cmd);
  36. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  37. StringBuffer sb = new StringBuffer();
  38. String line;
  39. while ((line = br.readLine()) != null) {
  40. sb.append(line).append("\n");
  41. }
  42. result = sb.toString();
  43. System.out.println("result: " + result);
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. return result;
  48. }
  49. public static String exeCmdMulti_1() {
  50. String result = "";
  51. String command = "slpk2vts --input /var/vts/input/Production_13.slpk --output /root/java/test/test_13 --tilesetId test_13 --referenceFrame melown2015";
  52. try {
  53. String[] cmd = new String[]{"/bin/sh", "-c", command};
  54. Process ps = Runtime.getRuntime().exec(cmd);
  55. BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
  56. StringBuffer sb = new StringBuffer();
  57. String line;
  58. while ((line = br.readLine()) != null) {
  59. sb.append(line).append("\n");
  60. }
  61. result = sb.toString();
  62. System.out.println("result: " + result);
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. return result;
  67. }
  68. }