【问题标题】:running a vbs file from java从java运行vbs文件
【发布时间】:2016-10-31 15:55:08
【问题描述】:

我在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 文件吗?

【问题讨论】:

  • 显然在您的情况下,windows 不知道如何执行此命令。请使用执行器和脚本名称:类似:cscript C:\\work\\selenium\\chrome\\test.vbs

标签: java vbscript runtime


【解决方案1】:
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】:

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

    【讨论】:

      【解决方案3】:
      Runtime.getRuntime().exec( "cscript E:/Send_Mail_updated.vbs" )
      

      【讨论】:

        【解决方案4】:
        try {
                Runtime.getRuntime().exec(new String[] {
                "wscript.exe", "C:\\path\\example.vbs"
                });        
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        

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

        【讨论】:

          【解决方案5】:

          完整代码在这里

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

          【讨论】:

          • 读取stdout和stderr的代码很可能死锁。要正确编码,您必须使用单独的线程来读取进程的输出流
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多