running a vbs file from java

大憨熊 提交于 2019-12-05 03:06:51

A vbs-Script isn't natively executable like a bat, cmd or exe-Program. You have to start the interpreter (vbs.exe?) and hand your script over as a parameter:

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

This is working fine where Send_Mail_updated.vbs is name of my VBS file

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

You can use the code above to run vbs file.

user3701639

The complete code here

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);
           }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!