java 远程执行bat_Java中执行Shell(.sh)和windows批量处理(.bat)

[Windows下批处理文件执行Java程序 保存为“*.bat”文件: @echo off javac Main.java java Main args0 pause]

【JAVA 中执行Shell】

jShell.java

/*

*  jShell.java

*  class jShell is used for executing shell command

*  USAGE:

*      jShell obj=new jShell(shellCommand);

*      obj.startErr();

*      obj.startOut();

*      obj.startIn();

*  You can Interupt I/O thread when nessasary:

*      obj.interruptErr();

*      obj.interruptOut();

*      obj.interruptIn();

*

*  BY Ahui Wang    Nankai U.    2007-05-12

*/

import java.io.*;

public class jShell {

Thread tIn;    //handle input of child process

Thread tOut;//handle output of child process

Thread tErr;//handle error output of child process

public jShell(String shellCommand){

Process child=null;    //child process

try{

child=Runtime.getRuntime().exec(shellCommand);

}

catch(IOException e){

e.printStackTrace();

}

if(child==null){

return;

}

final InputStream inputStream=child.getInputStream();

final BufferedReader brOut=

new BufferedReader(new InputStreamReader(inputStream));

tOut=new Thread(){    //initialize thread tOut

String line;

int lineNumber=0;

public void run(){

try{

while((line=brOut.readLine())!=null){

System.out.println(lineNumber+". "+line);

lineNumber++;

}

}

catch(IOException e){

e.printStackTrace();

}

}

};

final InputStream errorStream=child.getErrorStream();

final BufferedReader brErr=

new BufferedReader(new InputStreamReader(errorStream));

tErr=new Thread(){    //initialize thread tErr

String line;

int lineNumber=0;

public void run(){

try{

while((line=brErr.readLine())!=null){

System.out.println(lineNumber+". "+line);

lineNumber++;

}

}

catch(IOException e){

e.printStackTrace();

}

}

};

// read buffer of parent process' input stream

final BufferedReader reader =

new BufferedReader(new InputStreamReader(System.in));

final OutputStream outputStream = child.getOutputStream();

tIn=new Thread(){

String line;

public void run() {

try {

while (true) {

outputStream.write( (reader.readLine()+"/n").getBytes());

outputStream.flush();

}

}

catch (IOException e) {

e.printStackTrace();

}

}

};

}

public void startIn(){ //start thread tIn

if(tIn!=null){

tIn.start();

}

}

public void startErr(){ //start thread tErr

if(tErr!=null){

tErr.start();

}

}

public void startOut(){ //start thread tOut

if(tOut!=null){

tOut.start();

}

}

public void interruptIn(){ //interrupt thread tIn

if(tIn!=null){

tIn.interrupt();

}

}

public void interruptErr(){ //interrupt thread tErr

if(tErr!=null){

tErr.interrupt();

}

}

public void interruptOut(){ //interrupt thread tOut

if(tOut!=null){

tOut.interrupt();

}

}

}

CODE: mainC.java

----------------------------------------------------------------------------------

public final class mainC {

public static void main(String[] args) {

jShell shell=new jShell("ls -l");

shell.startErr();

shell.startIn();

shell.startOut();

}

}

RESULT:

---------------------------------------------------------------------------------

0. 总用量 44

1. -rwxrwxrwx    1 root     root          219  5月 12 10:41 ex.pl

2. -rwxrwxrwx    1 root     root          211  5月 12 10:39 ex.pl~

3. -rwxrwxrwx    1 root     root          150  5月 12 10:41 ex.sh

4. -rwxrwxrwx    1 root     root          124  5月 12 10:20 ex.sh~

5. -rwxrwxrwx    1 root     root         1198  5月 12 10:43 jShell$1.class

6. -rwxrwxrwx    1 root     root         1198  5月 12 10:43 jShell$2.class

7. -rwxrwxrwx    1 root     root         1222  5月 12 10:43 jShell$3.class

8. -rwxrwxrwx    1 root     root         2241  5月 12 10:43 jShell.class

9. -rwxrwxrwx    1 root     root         2720  5月 12 10:43 jShell.java

10. -rwxrwxrwx    1 root     root          544  5月 12 11:43 mainC.class

11. -rwxrwxrwx    1 root     root          170  5月 12 11:43 mainC.java

[目的:  在windows xp下编写bat文件,定时调用java工程中的main.执行特定的程序.   环境目录模拟: D:\Program Files\Apache\deploy\XXXXXXXX -- conf文件夹 -- lib文件

PS:

Process  process=Runtime.getRuntime().exec("");中产生停滞(阻塞,blocking),怎么解决?

---------------------------------------------------------------

这个是因为Runtime.getRuntime().exec()要自己去处理stdout和stderr的。

所以如果你想让程序正常运行的话,请务必将上述用别的线程流取走。

【JAVA中执行bat】

>test.bat

haha

exit  99

>RuntimeTest.java

public  class  RuntimeTest  {

public  static  void  main(String[]  args)  {

try  {

Process  process=Runtime.getRuntime().exec("test.bat");

StreamGobbler  errorGobbler  =  new  StreamGobbler(process.getErrorStream(),  "ERROR");

//  kick  off  stderr

errorGobbler.start();

StreamGobbler  outGobbler  =  new  StreamGobbler(process.getInputStream(),  "STDOUT");

//  kick  off  stdout

outGobbler.start();

process.waitFor();

System.out.println(process.exitValue());

}  catch(Exception  e)  {}

}

}

>StreamGobbler.java

import  java.io.BufferedReader;

import  java.io.IOException;

import  java.io.InputStream;

import  java.io.InputStreamReader;

import  java.io.OutputStream;

import  java.io.PrintWriter;

public  class  StreamGobbler  extends  Thread  {

InputStream  is;

String  type;

OutputStream  os;

StreamGobbler(InputStream  is,  String  type)  {

this(is,  type,  null);

}

StreamGobbler(InputStream  is,  String  type,  OutputStream  redirect)  {

this.is  =  is;

this.type  =  type;

this.os  =  redirect;

}

public  void  run()  {

try  {

PrintWriter  pw  =  null;

if  (os  !=  null)

pw  =  new  PrintWriter(os);

InputStreamReader  isr  =  new  InputStreamReader(is);

BufferedReader  br  =  new  BufferedReader(isr);

String  line=null;

while  (  (line  =  br.readLine())  !=  null)  {

if  (pw  !=  null)

pw.println(line);

System.out.println(type  +  ">"  +  line);

}

if  (pw  !=  null)

pw.flush();

}  catch  (IOException  ioe)  {

ioe.printStackTrace();

}

}

}

[1. 安装J2SE(推荐1.5版本)      java.sun.com/products/archive/ 2. 安装Ant     请使用1.6之后的版本,解压缩到本地路径即可。    ant.apache.org/ 3. 下载JSCH:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过Java调用.sh脚本,你可以使用Runtime类的exec方法来执行shell命令。可以使用以下代码来执行.sh脚本: String[] cmd = { "sh", "/path/to/script.sh", "parameter1", "parameter2" }; try { Process p = Runtime.getRuntime().exec(cmd); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println("line = " + line); } reader.close(); } catch (Exception e) { e.printStackTrace(); } 在这段代码,我们定义了一个字符串数组cmd,其第一个元素是"sh",表示使用shell执行命令,第二个元素是"/path/to/script.sh",表示要执行的.sh脚本的路径,后面的参数可以根据需要进行更改。然后通过Runtime类的exec方法执行这个命令,最后通过BufferedReader来读取命令的输出并打印出来。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [java调用shell脚本传参数](https://blog.csdn.net/weixin_57463074/article/details/127802692)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [使用java执行batsh脚本文件](https://blog.csdn.net/gxy6661159/article/details/128615151)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值