简体   繁体   中英

What is the maximum number of arguments that can be passed in a Runtime.exec()?

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: Java 中的运行时方法

Resultant Error msg:

VBS 错误

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

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://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/z2b05k8s(v=vs.84)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM