-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_java17.ps1
More file actions
166 lines (139 loc) · 5.88 KB
/
install_java17.ps1
File metadata and controls
166 lines (139 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# PowerShell Script to Install Java 17 and Configure Environment Variables
# Run this script as Administrator
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Java 17 Installation Script" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# Check if running as Administrator
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "ERROR: This script must be run as Administrator!" -ForegroundColor Red
Write-Host "Right-click PowerShell and select 'Run as Administrator'" -ForegroundColor Yellow
pause
exit 1
}
# Define download URL and paths
$jdkUrl = "https://api.adoptium.net/v3/binary/latest/17/ga/windows/x64/jdk/hotspot/normal/eclipse"
$downloadPath = "$env:TEMP\jdk17-installer.msi"
$installPath = "C:\Program Files\Eclipse Adoptium\jdk-17"
Write-Host "Step 1: Downloading Java 17 JDK..." -ForegroundColor Green
try {
# Download JDK installer
Invoke-WebRequest -Uri $jdkUrl -OutFile $downloadPath -UseBasicParsing
Write-Host "Download completed successfully!" -ForegroundColor Green
} catch {
Write-Host "ERROR: Failed to download Java 17" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
pause
exit 1
}
Write-Host ""
Write-Host "Step 2: Installing Java 17..." -ForegroundColor Green
Write-Host "This may take a few minutes. Please wait..." -ForegroundColor Yellow
try {
# Install JDK silently
$installArgs = @(
"/i",
"`"$downloadPath`"",
"/quiet",
"/norestart",
"ADDLOCAL=FeatureMain,FeatureEnvironment,FeatureJarFileRunWith,FeatureJavaHome",
"INSTALLDIR=`"$installPath`""
)
$process = Start-Process -FilePath "msiexec.exe" -ArgumentList $installArgs -Wait -PassThru
if ($process.ExitCode -eq 0) {
Write-Host "Java 17 installed successfully!" -ForegroundColor Green
} else {
Write-Host "WARNING: Installation completed with exit code: $($process.ExitCode)" -ForegroundColor Yellow
}
} catch {
Write-Host "ERROR: Installation failed" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
pause
exit 1
}
# Find the actual JDK installation directory
Write-Host ""
Write-Host "Step 3: Locating Java installation..." -ForegroundColor Green
$javaHomePath = $null
$possiblePaths = @(
"C:\Program Files\Eclipse Adoptium\jdk-17*",
"C:\Program Files\Java\jdk-17*",
"C:\Program Files\Temurin\jdk-17*"
)
foreach ($path in $possiblePaths) {
$found = Get-ChildItem -Path $path -Directory -ErrorAction SilentlyContinue | Select-Object -First 1
if ($found) {
$javaHomePath = $found.FullName
break
}
}
if (-not $javaHomePath) {
Write-Host "ERROR: Could not find Java installation directory" -ForegroundColor Red
Write-Host "Please check C:\Program Files\ for Java installation" -ForegroundColor Yellow
pause
exit 1
}
Write-Host "Found Java at: $javaHomePath" -ForegroundColor Green
# Set JAVA_HOME environment variable
Write-Host ""
Write-Host "Step 4: Setting JAVA_HOME environment variable..." -ForegroundColor Green
try {
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $javaHomePath, [System.EnvironmentVariableTarget]::Machine)
Write-Host "JAVA_HOME set to: $javaHomePath" -ForegroundColor Green
} catch {
Write-Host "ERROR: Failed to set JAVA_HOME" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
}
# Add Java to PATH
Write-Host ""
Write-Host "Step 5: Adding Java to PATH..." -ForegroundColor Green
try {
$currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
$javaBinPath = "$javaHomePath\bin"
# Check if Java bin is already in PATH
if ($currentPath -notlike "*$javaBinPath*") {
$newPath = "$currentPath;$javaBinPath"
[System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::Machine)
Write-Host "Java bin added to PATH" -ForegroundColor Green
} else {
Write-Host "Java bin already in PATH" -ForegroundColor Yellow
}
} catch {
Write-Host "ERROR: Failed to update PATH" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
}
# Clean up installer
Write-Host ""
Write-Host "Step 6: Cleaning up..." -ForegroundColor Green
Remove-Item -Path $downloadPath -Force -ErrorAction SilentlyContinue
# Refresh environment variables for current session
Write-Host ""
Write-Host "Step 7: Refreshing environment variables..." -ForegroundColor Green
$env:JAVA_HOME = $javaHomePath
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Installation Complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# Verify installation
Write-Host "Verifying installation..." -ForegroundColor Yellow
Write-Host ""
try {
$javaVersion = & java -version 2>&1
Write-Host "Java Version:" -ForegroundColor Cyan
Write-Host $javaVersion
Write-Host ""
Write-Host "JAVA_HOME: $env:JAVA_HOME" -ForegroundColor Cyan
Write-Host ""
Write-Host "SUCCESS: Java 17 is installed and configured!" -ForegroundColor Green
Write-Host ""
Write-Host "IMPORTANT: Close and reopen your terminal/PowerShell for changes to take effect." -ForegroundColor Yellow
} catch {
Write-Host "WARNING: Could not verify Java installation" -ForegroundColor Yellow
Write-Host "Please close and reopen your terminal, then run: java -version" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Cyan
pause