2012-03-05 62 views
9

一个VBS文件我有一个C:/work/selenium/chrome/ VBS文件test.vbs,我想从我的Java程序运行它,所以我尝试这样做,但没有运气:运行从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(); 
    } 
} 

如果我尝试用这种方法运行一些exe文件它运行良好,但是当我尝试运行VBS文件时,它说“不是有效的win32应用程序”。

任何想法如何从Java运行VBS文件?

+2

显然在你的情况下,windows不知道如何执行这个命令。请使用执行程序和脚本名称:cscript C:\\ work \\ selenium \\ chrome \\ test.vbs – NiranjanBhat 2012-03-05 05:46:53

回答

2

vbs-Script不像bat,cmd或exe-Program那样本地执行。你必须开始解释(vbs.exe?),并交出你的脚本作为参数:

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); 
    } 
} 

这是工作得很好,Send_Mail_updated.vbs是我的VBS文件的名称

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

完整的代码在这里

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

读取stdout和stderr的代码可能会死锁。要正确编写代码,您必须使用单独的线程来读取进程的输出流 – aeropapa17 2014-10-01 15:41:24

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

您可以使用上面的代码来运行vbs文件。