2012-03-05 31 views
9

Tôi có một test.vbs tập tin VBS trong C:/work/selenium/chrome/ và tôi muốn chạy nó từ chương trình Java của tôi, vì vậy tôi cố gắng này, nhưng không có may mắn:chạy một tập tin vbs từ java

public void test() throws InterruptedException { 
    Runtime rt = Runtime.getRuntime(); 
    try { 
     Runtime.getRuntime().exec("C:/work/selenium/chrome/test.vbs"); 
    } 
    catch(IOException e) { 
     e.printStackTrace(); 
    } 
} 

Nếu tôi cố gắng chạy một số tập tin exe với phương pháp này nó chạy tốt, nhưng khi tôi cố gắng chạy một tập tin VBS nó nói "không phải là một ứng dụng win32 hợp lệ".

Bất kỳ ý tưởng nào về cách chạy tệp VBS từ Java?

+2

rõ ràng trong cửa sổ trường hợp của bạn không biết cách thực hiện lệnh này. Vui lòng sử dụng trình thực thi cùng với tên tập lệnh: giống như: cscript C: \\ work \\ selenium \\ chrome \\ test.vbs – NiranjanBhat

Trả lời

2

Vbs-Script không thể thực thi được như dơi, cmd hoặc chương trình exe. Bạn phải bắt đầu phiên dịch (vbs.exe?) Và tay kịch bản của bạn qua như một tham số:

String script = "C:\\work\\selenium\\chrome\\test.vbs"; 
// search for real path: 
String executable = "C:\\windows\\...\\vbs.exe"; 
String cmdArr [] = {executable, script}; 
Runtime.getRuntime().exec (cmdArr); 
10
public static void main(String[] args) { 
    try { 
     Runtime.getRuntime().exec("wscript D:/Send_Mail_updated.vbs"); 
    } 
    catch(IOException e) { 
     System.out.println(e); 
     System.exit(0); 
    } 
} 

này đang làm việc tốt nơi Send_Mail_updated.vbs là tên của tập tin VBS tôi

2
Runtime.getRuntime().exec("cscript E:/Send_Mail_updated.vbs") 
-1

các mã hoàn chỉnh ở đây

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
public class VBTest { 
    public static void main(String[] args) { 
     try { 
      String line; 
      OutputStream stdin = null; 
      InputStream stderr = null; 
      InputStream stdout = null; 
      Process process = Runtime.getRuntime().exec("cscript E:/Send_Mail_updated.vbs"); 
      stdin = process.getOutputStream(); 
      stderr = process.getErrorStream(); 
      stdout = process.getInputStream(); 

       // "write" the parms into stdin 
       line = "param1" + "\n"; 
       stdin.write(line.getBytes()); 
       stdin.flush(); 

       line = "param2" + "\n"; 
       stdin.write(line.getBytes()); 
       stdin.flush(); 

       line = "param3" + "\n"; 
       stdin.write(line.getBytes()); 
       stdin.flush(); 

       stdin.close(); 

       // clean up if any output in stdout 
       BufferedReader brCleanUp = new BufferedReader (new InputStreamReader (stdout)); 
       while ((line = brCleanUp.readLine()) != null) { 
       System.out.println ("[Stdout] " + line); 
       } 
       brCleanUp.close(); 

       // clean up if any output in stderr 
       brCleanUp = 
       new BufferedReader (new InputStreamReader (stderr)); 
       while ((line = brCleanUp.readLine()) != null) { 
       System.out.println ("[Stderr] " + line); 
       } 
       brCleanUp.close(); 
      } 
      catch(IOException e) { 
       System.out.println(e); 
       //System.exit(0); 
      } 
    } 
} 
+0

Mã đọc stdout và stderr có khả năng bế tắc. Để mã này đúng cách, bạn phải sử dụng các luồng riêng biệt để đọc luồng đầu ra của quá trình – aeropapa17

0
try { 
     Runtime.getRuntime().exec(new String[] { 
     "wscript.exe", "C:\\path\\example.vbs" 
     });   
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

Bạn có thể sử dụng đoạn mã trên để chạy tệp vbs.

Các vấn đề liên quan