Java executes shell scripts and outputs execution

Source: Internet
Author: User

1. Script test.sh, placed in the/users/hdwang directory

#!/bin/sh/users/hdwangechols: 'ls'for (( i=1; i<=; i++));  Do Echo $ (expr31); Sleep 1s  Done echo JPS: ' Jps-l '

2.java Execution test.sh

 Public classExecshell { Public Static voidMain (string[] args) {Try{Process Pro= Runtime.getruntime (). EXEC ("/users/hdwang/test.sh"); InputStream in=Pro.getinputstream (); BufferedReader Read=NewBufferedReader (NewInputStreamReader (in)); String Line=NULL;  while(line = Read.readline ())! =NULL) {System.out.println (line);        } pro.waitfor (); } Catch(Exception e) {e.printstacktrace (); }    }}

3. Implementation results

Ls:app applications Desktop Documents Downloads Library Movies Music Pictures Projects public a.html C Curl.txt java_error _in_idea.hprof mavenrepo nginx Configuration res sessiontmp test.sh record the machine configuration environment, such as the machine configuration environment. zip
4
7
10
13
16
19
22
25
28
31
jps:11617 sun.tools.jps.Jps 11592 org.jetbrains.jps.cmdline.Launcher 10633 org.jetbrains.jps.cmdline.Launcher 11593 Com.hdwang.ExecShell 10539 Com.hzxc.chess.server.Booter 555

Java executes shell scripts and outputs execution

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.