错误:安装Java时无法对空数组进行索引

huangapple go评论86阅读模式
英文:

Error:- Cannot index into a null array while installing java

问题

我正在尝试在 Packer 实例上使用 PowerShell 的 Invoke-WebRequest 命令安装 Java,然而,我遇到了以下错误;

```powershell
无法索引空数组。
在第1行第94个字符处:+ ... content | %{[regex]::matches($_, ''(?:<a title="Download Java software ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

命令:

[Net.ServicePointManager]::SecurityProtocol = 'tls12'

$URL = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | %{[regex]::matches($_, ''(?:<a title="Download Java software for Windows .64-bit." href=")(.*)(?:">)').Groups[1].Value}

Invoke-WebRequest -UseBasicParsing -OutFile jre8.exe $URL

Start-Process .\jre8.exe '/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0' -wait

几周前我能够成功运行它,但是从昨天开始出现了上述错误。
有什么建议吗?

谢谢。


<details>
<summary>英文:</summary>

I am trying to install java using powershell Invoke-WebRequest command on packer instance however, I am getting below error;

Cannot index into a null array.
At line:1 char:94

  • ... content | %{[regex]::matches($_, '(?:<a title="Download Java software ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : NullArray
Command;

`[Net.ServicePointManager]::SecurityProtocol = &quot;tls12&quot;`

`$URL = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | %{[regex]::matches($_, &#39;(?:&lt;a title=&quot;Download Java software for Windows .64-bit.&quot; href=&quot;)(.*)(?:&quot;&gt;)&#39;).Groups[1].Value}`

`Invoke-WebRequest -UseBasicParsing -OutFile jre8.exe $URL`

`Start-Process .\jre8.exe &#39;/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0&#39; -wait`

Few weeks ago I was able to run it successfully but since yesterday getting the above error.
Any advise?

Thanks.

</details>


# 答案1
**得分**: 2

这是因为在网页上并不存在类似于“Download Java software for Windows”的字符串。由于正则表达式没有匹配到任何内容,“Groups”成员也不存在,所以当尝试索引一个不存在的成员时会出现错误。

你可以使用浏览器的“查看源代码”命令,或者将内容保存到文本文件中,然后像这样使用记事本查看:

    $cc = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content
    Set-Content -Path c:\temp\javapage.txt -Value $cc
    notepad c:\temp\javapage.txt

该页面加载了大量的Javascript代码,用于生成在浏览器中看到的实际页面。

<details>
<summary>英文:</summary>

This happens, as there is no such as string as `Download Java software for Windows` on the web page. Since the regex doesn&#39;t match anything, `Groups` member doesn&#39;t exist and you&#39;ll get an error about trying to index into a non-existing member.

Either use a web browser&#39;s View Source command, or save the content on a text file and view it with Notepad like so,

    $cc = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content
    Set-Content -Path c:\temp\javapage.txt -Value $cc
    notepad c:\temp\javapage.txt

The page loads a bunch of Javascript that generates the actual page seen on a browser.

</details>



huangapple
  • 本文由 发表于 2020年10月6日 14:58:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64220792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定