从java运行vbs文件

从java运行vbs文件,java,vbscript,runtime,Java,Vbscript,Runtime,我在C:/work/selenium/chrome/中有一个VBS文件test.VBS,我想从Java程序中运行它,所以我尝试了这个方法,但没有成功: public void test() throws InterruptedException { Runtime rt = Runtime.getRuntime(); try { Runtime.getRuntime().exec( "C:/work/selenium/chrome/test.vbs" ); }

我在
C:/work/selenium/chrome/
中有一个VBS文件test.VBS,我想从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文件吗

vbs脚本不像bat、cmd或exe程序那样本机可执行。您必须启动解释器(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);
这在
Send\u Mail\u updated.vbs
是我的vbs文件的名称

这里的完整代码时工作正常

Runtime.getRuntime().exec( "cscript E:/Send_Mail_updated.vbs" )
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);
           }
    }
}

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

显然,在您的情况下,windows不知道如何执行此命令。请将执行器与脚本名称一起使用:类似于:cscript C:\\work\\selenium\\chrome\\test.vbs读取标准输出和标准输出的代码可能会死锁。要正确地编写代码,必须使用单独的线程来读取进程的输出流
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);
           }
    }
}
try {
        Runtime.getRuntime().exec(new String[] {
        "wscript.exe", "C:\\path\\example.vbs"
        });        
    } catch (Exception ex) {
        ex.printStackTrace();
    }