Home > Enterprise >  How to run java program via vbscript with multiple arguments?
How to run java program via vbscript with multiple arguments?

Time:12-08

I want to run a simple java programme with parsing arguments using VB Scripts.

this is my VBScript code

Sub Button1_Click()
Set WshShell = CreateObject("WScript.Shell")
Dim jar
Dim location
Dim dictionary
Set dictionary = CreateObject("Scripting.Dictionary")
jar = "C:\Users\Documents\ExP.jar"
location = "C:\Users\Documents\TestProject"
Set WshShellExec = WshShell.Exec("java -jar " & Chr(34) & jar & Chr(34) & location & Chr(34))
End Sub

This is my java class which I want to pass arguments

    public static void main(String[] args) {
        ReadTestScripts readTestScripts = new ReadTestScripts(args[3]);
//location  ="C:\\Users\\Documents\\TestProject" - want to pass this path to java programme 
        readTestScripts.showResult();
    }

I didn't get the expected output when I pass the location as VBScript argument

CodePudding user response:

You are concatenating the command to execute but not leaving spaces between the arguments so at the moment if you debugged the command being passed to Exec() it will look like;

java -jar "C:\Users\Documents\ExP.jar""C:\Users\Documents\TestProject"

The fix is to add a concatentated space between jar and location.

Sub Button1_Click()
    Set WshShell = CreateObject("WScript.Shell")
    Dim a
    Dim location
    Dim dictionary
    Set dictionary = CreateObject("Scripting.Dictionary")
    jar = "C:\Users\Documents\ExP.jar"
    location = "C:\Users\Documents\TestProject"
    Set WshShellExec = WshShell.Exec("java -jar """ & jar & """ """ & location & """")
End Sub

Which will produce the command

java -jar "C:\Users\Documents\ExP.jar" "C:\Users\Documents\TestProject"

Note, the replacement of Chr(34) with the correct escape method for double quotes which is to double them.


Useful Links

  • Related