JAVA調用shell腳本實例


  ~/testcode/HelloWorld.java源碼文件java


  1 public class HelloWorld{
  2       // 程序的入口
  3     public static void main(String args[]){
  4       //       // 向控制檯輸出信息   
  5            System.out.println("歡迎java01班的同窗");
  6            test3();
  7     }
  8     private static void test3() {  
  9       execCommand1("sh /home/tony/testcode/test.sh");                                                                                              
 10       System.out.println("...........");
 11     }
 12
 13     public static boolean execCommand1(String cmd) {
 14         Process process = null;        
 15         try {
 16               process = Runtime.getRuntime().exec(cmd);
 17               process.waitFor();             
 18         } catch (Exception e) {        
 19               return false;
 20         } finally {
 21             try {
 22                   process.destroy();             
 23             } catch (Exception e) {        
 24               }
 25         }
 26           return true;
 27     }
 28 }
shell



測試的shell腳本文件 ~/testcode/test.shbash


  1 #/bin/bash
  2 echo "test one">/home/tony/testcode/007.txt
測試



測試結果會在指定目錄生成007.txt文件code

相關文章
相關標籤/搜索