3

We have an application that is Java dependent that we are working on a SCCM installer to push out to clients.

Due to Java being updated almost monthly (and the old updates expiring 30 days thereafter) we're looking for a way to have the SCCM installer stay updated with the latest Java version. We have the ability to run a Powershell script from our SCCM installer, but we haven't found a way to write a script that will automatically download and install the latest Java version.

Does anyone know if this is possible, and how we could go about it?

Thanks in advance!!

korymiller
  • 357
  • 4
  • 9
  • 21
  • You question could be considered off topic since you are asking about creating a software installation package. While this is possible... if you want help with this I suggest that you do show us some code you have tried and then the community will help you. – Matt Oct 28 '14 at 15:15
  • The software installation package isn't the topic of the question. Writing a script to download Java is, as stated above we have not found any way to download the latest version of Java so we don't have any code to show. Thank you for your response. – korymiller Oct 28 '14 at 17:57
  • Your question does state _automatically download and install the latest Java version_ which is why i responded the way i did FYI – Matt Oct 28 '14 at 18:13

4 Answers4

2

Please have a look at this for downloading a file with powershell and this for installing java silent. For example:

Invoke-WebRequest $address -OutFile $destination
# $addressis the http-address of the installer
# $destination is a Path where the the downloadoutput should saved to
$destination /s
# with the '/s' Parameter you are starting the installer silent

If you can get the download link, you should be able to download and install java. But atm i have no idea how you can get the link. I hope this helps a little bit.

Matt
  • 45,022
  • 8
  • 78
  • 119
Alex H
  • 1,424
  • 1
  • 11
  • 25
  • You need to include the useful parts of these links. If they become dead later your answer would be of no use to anyone. – Matt Oct 28 '14 at 18:14
1

You can probably use the links available on this page for the download: http://java.com/en/download/manual.jsp

Alternatively, you can do the download once and put it on a file share so you can use simple methods for copying the file from a UNC path like

\\fileserver\folder\installerpackage.exe

Just be prepared for headaches. Having dealt with removing and upgrading Java on dozens of machines myself, I can tell you it's not always a clean uninstall/reinstall. Unless they've improved the uninstaller, there's a good chance you will eventually hit a situation where Java's installer thinks it's installed, but it's not. I've also seen the opposite where it thinks it's not installed but it is.

Booga Roo
  • 1,665
  • 1
  • 21
  • 30
0

First you need to get the latest Download Link(32-bit).

$JavaFile = [pscustomobject]@{
    JavaVersion = ''
    FileName = ''
    DownloadURL =  $((Invoke-WebRequest 'http://www.java.com/en/download/manual.jsp').links | where innerHTML -like "Windows Offline" | select href).href
} 

For 64-bit URL use:

$JavaFile = [pscustomobject]@{
    JavaVersion = ''
    FileName = ''
    DownloadURL =  $((Invoke-WebRequest 'http://www.java.com/en/download/manual.jsp').links | where innerHTML -like "Windows Offline (64-bit)" | select href).href
} 

So now we want to check if we already downloaded the file. We can do this by checking a log file. So first create a javahistorylog.log for storing used links. With the following Function we can Check the log for links.

function CheckHistory ($fileurl,$path){
    $history=Get-Content "$path"
    $r = $false
    Foreach ($historicurl in $history){
        if ($historicurl -eq $fileurl){
            $r = $true
        }
    }
    return $r
}

If it wasn't found we can continue with downloading the File.

$JavaFile.FileName = "tempinstaller$(get-date -Format yyMMddmm).exe"
Invoke-WebRequest $JavaFile.DownloadURL -OutFile ("$TempDownloadLocation\"+ $JavaFile.FileName) -ev $DLErr
if($DLErr){Exit}

After the Download we can do some edits to the setup.exe First get the Version and safe the name.

$TempFileName = $JavaFile.FileName
$JavaFile.JavaVersion = get-item ("$TempDownloadLocation\"+ $JavaFile.FileName) | select -ExpandProperty versioninfo | select -ExpandProperty productversion

Second set and generate the name out of the originalfilename prop.

$JavaFile.FileName = "jre1."+(((Select-String -Pattern '[0-9]u[0-9]+' -InputObject (get-item ("$TempDownloadLocation\$TempFileName")   | select -ExpandProperty versioninfo | select -ExpandProperty originalfilename)) |
ForEach-Object -Process {
  $_.Matches
} |
ForEach-Object -Process {
  $_.Value
}) -replace 'u', '.0_')

now rename the File.

Rename-Item -Path "$TempDownloadLocation\$TempFileName" -NewName ($JavaFile.FileName+".exe")

update the historylog

if(Test-Path -path ("$TempDownloadLocation\"+$JavaFile.FileName+".exe")){
    add-content "$TempDownloadLocation\javahistorylog.log" $JavaFile.DownloadURL
}

Now we can think about getting the MSI File for building a sw package.

The MSI will be located at:

$MsiFilePathTemp = "$env:LOCALAPPDATA\Oracle\Java\" -replace 'Local', 'LocalLow'

We can get the MSI by executing the setup and coping the msi:

Start-Process -FilePath ("$TempDownloadLocation\"+$JavaFile.FileName+".exe") -ArgumentList '/s'
while(!(Test-Path ("$MsiFilePathTemp\"+$JavaFile.FileName+"\"+$JavaFile.FileName+".msi")))
{
  Start-Sleep -Seconds 1.5
}
Copy-Item -Path ("$MsiFilePathTemp\"+$JavaFile.FileName+"\"+$JavaFile.FileName+".msi") -Destination $MsiFilePathOut -Force -ErrorVariable $CDR
if($CDR){
    exit
}

Now Stop the Process and delete the .exe file

Get-Process -Name $JavaFile.FileName | Stop-Process
While (Get-Process -Name $JavaFile.FileName -ErrorAction SilentlyContinue)
{
  Start-Sleep -Seconds 2
}
Remove-Item -Path ("$TempDownloadLocation\"+$JavaFile.FileName+".exe") -Force
$MSIFile = ("$MsiFilePathOut\"+$JavaFile.FileName+".msi")

Now you have $MSIFile which is the path of the msi you could use for building your SCCM Package.

Mathis Michel
  • 277
  • 2
  • 7
0

I was facing a similar issue in one of my project which had a Java dependency and it had to be updated from Java 8 to Java 17. Following is the Windows Powershell script. You can replace your specific version.

$version = "17.0.2";
$zip_url = "https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_windows-x64_bin.zip";
$sha_url = "https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_windows-x64_bin.zip.sha256"

$java_folder = "C:\ProgramFiles\windows\Java_Temurin-Hotspot_jdk\17.0.2"

# Windows seems to auto-rename folders like "20.0.0" to "20".
$jdk_folder = $java_folder

$bin_folder = $jdk_folder + '\bin'

# Create Java folder.
New-Item -ItemType directory -Path $java_folder -Force
Set-Location $java_folder

# Download JDK ZIP & SHA256 Files
$jdk_zip_file = 'jdk.zip'
$jdk_sha_file = 'sha.sha256'

Invoke-WebRequest -Uri $zip_url -OutFile $jdk_zip_file
Invoke-WebRequest -Uri $sha_url -OutFile $jdk_sha_file

# Extract Archive
Expand-Archive -Path $jdk_zip_file
Remove-Item -Path $jdk_zip_file
Remove-Item -Path $jdk_sha_file

$currentPath = [Environment]::GetEnvironmentVariable('Path', 'Machine')
$newPath = $currentPath + ';' + $bin_folder

# Set the Java bin directory as the default
[Environment]::SetEnvironmentVariable('Path', $newPath, 'Machine')

[Environment]::SetEnvironmentVariable('JAVA_HOME', $jdk_folder, 'Machine')
[Environment]::SetEnvironmentVariable('JDK_HOME', $jdk_folder, 'Machine')
[Environment]::SetEnvironmentVariable('JRE_HOME', $jdk_folder, 'Machine')
 
# To verify the updated JAVA_HOME & Path Environment variables
$javaHome = [Environment]::GetEnvironmentVariable('JAVA_HOME', 'Machine')
Write-Output "JAVA_HOME Variable: $javaHome"

$pathVar = [Environment]::GetEnvironmentVariable('Path', 'Machine')
Write-Output "Path Variable: $pathVar"

java -version

In some cases it might happen that there is an older version of Java installed on your machine & despite your installation still the older version is prevalent. In that particular case you can remove the existing path from the Path environment variable to make sure that the newer version is picked up by your project. Here is the script to do that

# Define the path you want to remove
$pathToRemove = "C:\ProgramFiles\windows\Java_Temurin-Hotspot_jdk\8.0.382-5\x64\bin"

# Split the current PATH into individual paths
$paths = $currentPath -split ';'

# Filter out the path you want to remove
$filteredPaths = $paths | Where-Object { $_ -ne $pathToRemove }

# Join the filtered paths back into a single string
$updatedPath = $filteredPaths -join ';'
  

Hope this works for you.

srijan439
  • 401
  • 5
  • 7