Java invoke shell script

Source: Internet
Author: User

In the actual project, Java sometimes needs to call C write out of things, in addition to JNI, I think a better way is Java call the shell. First write the C to make the executable file, and then write a shell script to execute the executable file, and finally Java calls the shell script.

The Java invocation is simple, with the following examples:

The first is the shell script

[Plain]View plain copy print?
    1. #!/bin/sh
    2. echo Begin Word cluster
    3. /home/felven/word2vec/word2vec-train/home/felven/word2vec/resultbig.txt-output/home/felven/word2vec/ Classes.csv-cbow 0-size 200-window 5-negative 0-hs 1-sample 1e-3-threads 12-classes 2000
    4. echo the word classes were saved to file Classes.csv


Then the Java calling code

[Java]View plain copy print?
  1. Import Java.io.BufferedReader;
  2. Import Java.io.InputStreamReader;
  3. Public class Runshell {
  4. public static void Main (string[] args) {
  5. try {
  6. String shpath="/home/felven/word2vec/demo-classes.sh";
  7. Process PS = Runtime.getruntime (). exec (Shpath);
  8. Ps.waitfor ();
  9. BufferedReader br = new BufferedReader (new InputStreamReader (Ps.getinputstream ()));
  10. StringBuffer sb = new StringBuffer ();
  11. String Line;
  12. While (line = Br.readline ()) = null) {
  13. Sb.append (line) append ("\ n");
  14. }
  15. String result = Sb.tostring ();
  16. SYSTEM.OUT.PRINTLN (result);
  17. }
  18. catch (Exception e) {
  19. E.printstacktrace ();
  20. }
  21. }
  22. }


It is actually a process class that makes the call and then outputs the shell execution results to the console.

It is important to note that the WAITFOR () function needs to be executed at invocation, because the shell process is a child of the Java process, and Java as the parent process needs to wait for the child process to finish executing.

In addition to the Eclipse console output is not the side of the execution side output, but the shell after the completion of all the output, so if the more complex shell script to see no output may be mistaken for execution, this time to look at the terminal process, With the top command you can see that the shell script has actually started executing.

Java invoke shell script

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.