Downloading jdk using powershell
Solution 1
On inspecting the session on oracle site the following cookie catches attention: oraclelicense=accept-securebackup-cookie
. With that in mind you can run the following code:
$source = "http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-i586.exe"
$destination = "C:\Download\Java\jdk-7u60-windows-i586.exe"
$client = new-object System.Net.WebClient
$cookie = "oraclelicense=accept-securebackup-cookie"
$client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie)
$client.downloadFile($source, $destination)
Solution 2
EDIT: here's the reason for your problem: you can't directly download the file without accepting the terms before.
I'm using the following script to download files. It's working with HTTP as well as with FTP. It might be a little overkill for your task because it also shows the download progress but you can trim it untill it fits your needs.
param(
[Parameter(Mandatory=$true)]
[String] $url,
[Parameter(Mandatory=$false)]
[String] $localFile = (Join-Path $pwd.Path $url.SubString($url.LastIndexOf('/')))
)
begin {
$client = New-Object System.Net.WebClient
$Global:downloadComplete = $false
$eventDataComplete = Register-ObjectEvent $client DownloadFileCompleted `
-SourceIdentifier WebClient.DownloadFileComplete `
-Action {$Global:downloadComplete = $true}
$eventDataProgress = Register-ObjectEvent $client DownloadProgressChanged `
-SourceIdentifier WebClient.DownloadProgressChanged `
-Action { $Global:DPCEventArgs = $EventArgs }
}
process {
Write-Progress -Activity 'Downloading file' -Status $url
$client.DownloadFileAsync($url, $localFile)
while (!($Global:downloadComplete)) {
$pc = $Global:DPCEventArgs.ProgressPercentage
if ($pc -ne $null) {
Write-Progress -Activity 'Downloading file' -Status $url -PercentComplete $pc
}
}
Write-Progress -Activity 'Downloading file' -Status $url -Complete
}
end {
Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged
Unregister-Event -SourceIdentifier WebClient.DownloadFileComplete
$client.Dispose()
$Global:downloadComplete = $null
$Global:DPCEventArgs = $null
Remove-Variable client
Remove-Variable eventDataComplete
Remove-Variable eventDataProgress
[GC]::Collect()
}
Solution 3
After needing this script for a platform installer that required the JDK as a dependency:
<# Config #>
$DownloadPageUri = 'https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html';
$JavaVersion = '8u201';
<# Build the WebSession containing the proper cookie
needed to auto-accept the license agreement. #>
$Cookie=New-Object -TypeName System.Net.Cookie;
$Cookie.Domain='oracle.com';
$Cookie.Name='oraclelicense';
$Cookie.Value='accept-securebackup-cookie';
$Session= `
New-Object -TypeName Microsoft.PowerShell.Commands.WebRequestSession;
$Session.Cookies.Add($Cookie);
<# Fetch the proper Uri and filename from the webpage. #>
$JdkUri = (Invoke-WebRequest -Uri $DownloadPageUri -WebSession $Session -UseBasicParsing).RawContent `
-split "`n" | ForEach-Object { `
If ($_ -imatch '"filepath":"(https://[^"]+)"' { `
$Matches[1] `
} `
} | Where-Object { `
$_ -like "*-$JavaVersion-windows-x64.exe" `
}[0];
If ($JdkUri -imatch '/([^/]+)$') {
$JdkFileName=$Matches[1];
}
<# Use a try/catch to catch the 302 moved temporarily
exception generated by oracle from the absance of
AuthParam in the original URL, piping the exception's
AbsoluteUri to a new request with AuthParam returned
from Oracle's servers. (NEW!) #>
try {
Invoke-WebRequest -Uri $JdkUri -WebSession $Session -OutFile "$JdkFileName"
} catch {
$authparam_uri = $_.Exception.Response.Headers.Location.AbsoluteUri;
Invoke-WebRequest -Uri $authparam_uri -WebSession $Session -OutFile "$JdkFileName"
}
Works in PowerShell 6.0 (Hello 2019!) Required a minor fixup to the regex and the way the -imatch line scan happened. Workaround to follow 302 redirects was located here: https://github.com/PowerShell/PowerShell/issues/2896 (302 redirect workaround by fcabralpacheco readapted to get Oracle downloads working again !
This solution downloads 8u201 for Windows x64 automatically.
user1400915
Updated on June 25, 2022Comments
-
user1400915 almost 2 years
I am trying to download the java jdk using powershell scripting as given in the link below
. Here as the author has specified , if I Change the source url where the latest jdk is present i.e.,
http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-x64.exe
the content is not getting loaded , only about 6KB gets downloaded . I have a doubt , whether the download limit in powershell script is only 6KB?
Here is the code :
$source = "http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-windows-i586.exe" $destination = "C:\Download\Java\jdk-7u60-windows-i586.exe" $client = new-object System.Net.WebClient $client.DownloadFile($source, $destination)
-
user1400915 almost 10 yearsThanks for the code , but unfortunately it is not downloading the complete exe, only 6KB is getting downloaded . The url I am using is : download.oracle.com/otn-pub/java/jdk/8u5-b13/…
-
MichaelS almost 10 yearsThis is what I'm getting on openning the URL:Sorry! In order to download products from Oracle Technology Network you must agree to the OTN license terms. Be sure that... Your browser has "cookies" and JavaScript enabled. You clicked on "Accept License" for the product you wish to download. You attempt the download within 30 minutes of accepting the license. From here you can go... Back to Previous Page Site Map OTN Homepage
-
MichaelS almost 10 yearsSo here's the reason for your problem: you can't directly download the file without accepting the terms before.
-
user1400915 almost 10 yearsThose two parameters are set in the IE browser dude , even then I am getting the same thing, can you once verify if possible try to download jdk from oracle site please, then later I will recheck the problem from my end...
-
MichaelS almost 10 yearsSame problem here. Download via browser works fine but the script doesn't work.
-
user1400915 almost 10 yearsSo what to do now? how can anybody download from oracle site?
-
MichaelS almost 10 yearsBut now I'm sure that the problem is the terms agreement. If you save the error website it has is exactly the size of the file you are downloading. You can even rename the downloaded file from .exe to .html and open it in your browser.
-
MichaelS almost 10 yearsGot an idea but it's dirty. You can use a tool like JMeter to record a macro of HTTP-calls leading you over accepting the terms to the download and try to reproduce the path by directly sending the HTTP requests. No idea whether it works.
-
Steve Coleman over 5 yearsI am not sure who down voted this. but it works fine. Check the DownloadPageUri, javaversion, and JavaVersion-windows-x64.tar.gz are still valid. If oracle changes anything this will stop working.
-
Steve Coleman over 5 yearsTry setting $JavaVersion = '8u191', This is going to break every time they update their version info.
-
Matas Vaitkevicius about 4 years
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request." At line:1 char:1 + $client.downloadFile($source, $destination) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
-
Raf about 4 yearsHey @MatasVaitkevicius, it would be better if you raise this a separate question - the original post is nearly 6 years old...