Java calls shell scripts

Source: Internet
Author: User

I just got started with Linux development and recently encountered the problem of calling shell scripts in Java. I found a method suitable for cainiao-level development, as described below:

 

Add # In the first line of the shell script to be run #! /Bin/sh

Then run chmod A + X test. Sh on the terminal to convert test. Sh to an executable file.

Note that the directories run in Java may be different from those run by shell users. We recommend that you use full paths, such as/root/bin/test. Sh.

 

AllCode

Bytes ------------------------------------------------------------------------------------------------------------------------------

Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. Io. linenumberreader;

Public class runshell {

Public static void main (string [] ARGs ){
Try {
Process = runtime.getruntime(cmd.exe C ("/home/TINA/desktop/cal_time.sh ");
Inputstreamreader IR = new inputstreamreader (process. getinputstream ());
Linenumberreader input = new linenumberreader (IR );
String line;
While (line = input. Readline ())! = NULL)
System. Out. println (line );
Input. Close ();
IR. Close ();
} Catch (ioexception e ){
// Todo: handle exception
E. printstacktrace ();
}
}
}
Bytes -----------------------------------------------------------------------------------------------------------------------------

 

The script can be correctly executed. The code is running and the verification is correct.

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.