running a vbs file from java

45,136

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

Runtime.getRuntime().exec( "cscript E:/Send_Mail_updated.vbs" )
Share:
45,136
user1226162
Author by

user1226162

Updated on July 09, 2022

Comments

  • user1226162
    user1226162 almost 2 years

    I have a VBS file test.vbs in C:/work/selenium/chrome/ and I want to run it from my Java program, so I tried this but with no luck:

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

    If I try to run some exe file with this method it runs well, but when I try to run a VBS file it says "not a valid win32 application".

    Any idea how to run a VBS file from Java?

  • aeropapa17
    aeropapa17 over 9 years
    The code which reads the stdout and stderr is likely to deadlock. To code this properly, you must use separate threads to read the output streams of the process