java执行bat跟shell

java执行bat和shell

1. 请教关于JAVA在WIN下执行.BAT文件的问题 ->6楼的回答

2.请问如何在调用cmd运行完bat文件后自动退出 ->11楼的回答

3.java调用bat

在前面的博客中写道了mysql的数据库备份与恢复,这个主要是通过手动调用bat文件来执行的,那么如何在程序中调用bat文件呢。这就是这篇博客需要讲的主题。

2.实例1:

java执行bat跟shell
import java.io.IOException;

public class InvokeBat4 {
public void runbat(String batName) {
String cmd = "cmd /c start F:\\database_backup\\ngx_backup\\"+ batName + ".bat";// pass
try {
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
} catch (IOException ioe) {
ioe.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("child thread donn");
}

public static void main(String[] args) {
InvokeBat4 test1 = new InvokeBat4();
test1.runbat("backup_ngx");
System.out.println("main thread");
}
}
java执行bat跟shell

2.1.缺点:

会弹出cmd框,并且不能自动关闭

2.2.解决方法:

在bat文件最后加上

exit

比如原来的bat文件内容如下:

mysqldump -uroot -proot --database ngx_ad ngx_authority ngx_jbpm ngx_mes ngx_model > F:\database_backup\ngx_backup\ngx_db.sql

我们将其修改为

mysqldump -uroot -proot --database ngx_ad ngx_authority ngx_jbpm ngx_mes ngx_model > F:\database_backup\ngx_backup\ngx_db.sql
exit

2.3.能够自动退出但依然存在的问题

即使能够自动退出,但是每次调用这个bat的时候屏幕总是会闪一下cmd命令框。

3.改进实例,直接执行bat

java执行bat跟shell
import java.io.IOException;
import java.io.InputStream;

public class InvokeBat2 {
public void runbat(String batName) {
try {
Process ps = Runtime.getRuntime().exec(batName);
InputStream in = ps.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.print(c);// 如果你不需要看输出,这行可以注销掉
}
in.close();
ps.waitFor();

} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("child thread done");
}

public static void main(String[] args) {
InvokeBat2 test1 = new InvokeBat2();
String batName = "F:\\database_backup\\ngx_backup\\backup_ngx.bat";
test1.runbat(batName);
System.out.println("main thread");
}
}

1。 bat文件

import java.io.IOException;
import java.io.InputStream;


public class callbat {
 
 
      public static void main(String args[]){
         callCmd("C:/run.bat");
      }
      public static void  callCmd(String locationCmd){
          try {
          Process child = Runtime.getRuntime().exec("cmd.exe /C start "+locationCmd);
          InputStream in = child.getInputStream();
          int c;
          while ((c = in.read()) != -1) {
      }
       in.close();
       try {
           child.waitFor();
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       System.out.println("done");
     } catch (IOException e) {
           e.printStackTrace();
     }
 }
 }

 

 

2.shell文件

 

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class callshell {
 
  public static void main(String args[]) throws IOException{
   
  Runtime rt=Runtime.getRuntime();
  String command="/export/home/xlg/solarischk.sh";
  Process pcs=rt.exec(command);
  PrintWriter outWriter=new PrintWriter(new File("/export/home/zjg/show.txt"));
  BufferedReader br = new BufferedReader(new InputStreamReader(pcs.getInputStream()));
  String line=new String();
  while((line = br.readLine()) != null)
  {
  System.out.println(line);
  outWriter.write(line);
  }
  try{
  pcs.waitFor();
  }
  catch(InterruptedException e){
  System.err.println("processes was interrupted");
  }
  br.close();
  outWriter.flush();
  outWriter.close();
  int ret=pcs.exitValue();
  System.out.println(ret);
  System.out.println("执行完毕!");
  }
  }

 

  1.      * 运行shell 
  2.      *  
  3.      * @param shStr 
  4.      *            需要执行的shell 
  5.      * @return 
  6.      * @throws IOException 
  7.      */  
  8.     public static List runShell(String shStr) throws Exception {  
  9.         List<String> strList = new ArrayList();  
  10.   
  11.         Process process;  
  12.         process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",shStr},null,null);  
  13.         InputStreamReader ir = new InputStreamReader(process  
  14.                 .getInputStream());  
  15.         LineNumberReader input = new LineNumberReader(ir);  
  16.         String line;  
  17.         process.waitFor();  
  18.         while ((line = input.readLine()) != null){  
  19.             strList.add(line);  
  20.         }  
  21.           
  22.         return strList;  
  23.     }  

 

注:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流