Java calls the shell to get the return value

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/tengdazhang770960436/article/details/12014839

1.shell file return.sh

Echo 1

Echo 2

Echo 3

2.java file Test.java

[Java]View PlainCopy
  1. Import Java.io.BufferedInputStream;
  2. Import Java.io.BufferedReader;
  3. Import Java.io.InputStreamReader;
  4. Public class Test {
  5. public static void Main (string[] args) throws Exception {
  6. //define parameters for incoming shell script, put parameters into string array
  7. String cmds[] = new string[9];
  8. cmds[0] = "/home/aiuap_cj/report/return.sh";
  9. cmds[1] = "1"; //Task province ID
  10. cmds[2] = "2"; Insert the identity of the tax principal to which the task person belongs
  11. cmds[3] = "3"; Task duration
  12. cmds[4] = "4"; Statistics list Type (1: Calculation table, 2: Declaration form)
  13. cmds[5] = "5"; Insert the task person's level two company ID
  14. cmds[6] = "6"; Table name
  15. cmds[7] = "7";
  16. cmds[8] = "8"; Library name
  17. //Execute shell script
  18. Process pcs = Runtime.getruntime (). exec (CMDS);
  19. //define shell return values
  20. String result = null;
  21. //Get Shell return stream
  22. Bufferedinputstream in = new Bufferedinputstream (Pcs.getinputstream ());
  23. //character stream to convert byte streams
  24. BufferedReader br = new BufferedReader (new InputStreamReader (in));
  25. //The text log can also be output here
  26. String Linestr;
  27. While ((Linestr = Br.readline ()) = null) {
  28. result = Linestr;
  29. }
  30. //Close input stream
  31. Br.close ();
  32. In.close ();
  33. System.out.println ("==============================" + result);
  34. }
  35. }


Explain:

The value that the shell returns to Java is done through ECHO, and the shell return value that Java obtains is the last echo value, so the value that Java above can get is 3.

If you want to record errors in the execution of a script, you can change the return.sh as follows:

Echo 1

Echo 2

Echo 3

Rm-r/8888.txt 2>error.log

One of the problems here is that you cannot return the error message from the command execution to the Java caller directly, you can return the command execution result to the Java caller, then write the resulting error log to the development file, and then read the log file to see the error message.

Echo 1
Echo 2
Echo 3

Rm-r/8888.txt 2>error.log

echo $?

Java calls the shell to get the return value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.