1

I'm trying to pass a good number of arguments to a Runtime.().exec:

Runtime.getRuntime().exec(new String[] { executable, script, fnamePath, blah, blah, .... });

And the script after adding 12th argument says:

Error: Subscript out of range
Code: 800A0009

Can you please let me know what is the best way to pass mass arguments? Or, please rectify my method to achieve passing mass arguments.

Please let me know if any further details required...

VBS code:

Set objOutlook = CreateObject("Outlook.Application")
'Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set myMail = objOutlook.CreateItem(0)
Set Arg = WScript.Arguments
myMail.Attachments.Add Arg(0) 'Just to let you know I'm using the Arg(0) as well
IMED = Arg(1)
URL = Arg(2)
dashLoad = Arg(3)
roles = Arg(4)
consent = Arg(5)
dash = Arg(6)
servMenu = Arg(7)
folowUp = Arg(8)
servReq = Arg(9)
SRN = Arg(10)
PoP = Arg(11)
Doc = Arg(12)
SalesDashLoad = Arg(13)
MsgBox (SalesDashLoad) ' THIS LINE gives me error, till Agr(12) works fine!

Executing above VBS in Java: Runtime Method in Java

Resultant Error msg:

VBS Error

user692942
  • 16,398
  • 7
  • 76
  • 175
Koustav
  • 33
  • 6
  • 2
    The code you posted doesn't look like it's VBScript in the first place. For help with a VBScript error you need to show your VBScript code (more specifically a [mcve] of it). – Ansgar Wiechers Jul 09 '19 at 14:17
  • You say *"after the 12th argument"* but from VBScript code, it's expecting 14 arguments *(as the collection starts at `0`)*. You need to check you are passing 14 arguments or `Arg(13)` will fail with the `Subscript out of range` error. Go back and check you have 14 arguments being passed in. – user692942 Jul 10 '19 at 11:53
  • See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. Also break a `String arg` into `String[] args` to account for things like paths containing space characters. – Andrew Thompson Jul 11 '19 at 05:54

2 Answers2

1

There is no limit on passed arguments, but that isn't the issue.

You need to remember that the Java call to Runtime.getRuntime().exec() is first firing the VBScript Hosted executable (might be cscript.exe or wscript.exe) and passing the script file path to execute, this takes up two arguments in the command array you send to exec().

From the screenshot after the executable and script command array arguments I only see 13 arguments being passed but the VBScript is expecting 14 (WshArguments object collection uses a zero-based index).

As @Yuanyo mentions above you are missing SalesDashload from the argument list you're passing making it the 14th argument which would map it to Arg(13) in the VBScript.

Corrected input would be, something like (obviously I don't know what your variables are called can only guess or infer based on your existing ones);

Runtime.getRuntime().exec(new String[] {executable, script, fnamePath, loginScr1, stLink1, dashLoad1, role1, consent1, dash1, servMenu1, followUp1, servReq1, SRN1, PoP1, docSubmit1, salesDashLoad1 });

You could have captured this in the VBScript by using Arg.Count to check you had 14 arguments before continuing the script and maybe throwing an error if you don't or exiting the script.

Dim objOutlook, myMail, Arg
Const ExpectArgCount = 14
Set objOutlook = CreateObject("Outlook.Application")
'Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set myMail = objOutlook.CreateItem(0)
Set Arg = WScript.Arguments

If Arg.Count = ExpectArgCount Then
  myMail.Attachments.Add Arg(0)
  IMED = Arg(1)
  URL = Arg(2)
  dashLoad = Arg(3)
  roles = Arg(4)
  consent = Arg(5)
  dash = Arg(6)
  servMenu = Arg(7)
  folowUp = Arg(8)
  servReq = Arg(9)
  SRN = Arg(10)
  PoP = Arg(11)
  Doc = Arg(12)
  SalesDashLoad = Arg(13)
  MsgBox (SalesDashLoad)
Else
  Call Err.Raise(vbObjectError + 1, "My Application", "Incorrect number of arguments passed")
End If
user692942
  • 16,398
  • 7
  • 76
  • 175
0

Problem seems to be the index, you use. You are missing the parameter for SalesDashLoad.

You should have something like: Runtime.getRuntime().exec(new String[] {executable, script, fnamePath, loginScr1, stLink1, dashLoad1, role1, consent1, servMenu1, followUp1, servReq1, SRN1, PoP1, docSubmit1, salesDLoad1});

The Arguments property contains the WshArguments object (a collection of arguments). Use a zero-based index to retrieve individual arguments from this collection.

You can read more here: https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/z2b05k8s(v=vs.84)

Yuanyo
  • 111
  • 7
  • Unfortunately, I struggled to follow your answer because it still only contains 13 arguments when 14 are expected and I think that is because you are missing the `dash` argument from your list. – user692942 Jul 10 '19 at 12:11
  • Yeah, dash is missing, sorry. IF you add it you should have it working. – Yuanyo Jul 10 '19 at 13:11