I don’t know is there any other way for the environment variables more practical, I made a powershell script to automate this process.
Limitations:
Your environment variable must have the name of JAVA_HOME
if it is another name it is necessary to replace the occurrences of JAVA_HOME
by the name of its variable.
Some java installations do not have the instation location so they will not be displayed.
clear
write-host "Aguarde..."
[object[]]$r = (Get-WmiObject win32_product -Filter " name like 'JAVA %'")
$javaHome = "JAVA_HOME"
Write-Host "Valor atual do JAVA_HOME: $env:JAVA_HOME `n"
$opcao = 0
$itensValidos = @()
foreach($item in $r){
if($item.InstallLocation -ne $null){
Write-Host $opcao - $item.Name - $item.InstallLocation
$itensValidos += $item
$opcao++
}
}
$selecionado = Read-Host "`nInforme qual instalação do java será definida em JAVA_HOME"
if($selecionado -lt 0 -or $selecionado -gt $itensValidos.Count -1){
Write-Host "Opção invalida"
}
[Environment]::SetEnvironmentVariable($javaHome, $itensValidos[$selecionado].InstallLocation +"lib" , 'Machine')
Functioning
It is made the search of all the installed software that have JAVA
name, the following lines display the menu with the valid java installations (the ones that have the path of the survey), then a request is made to be the path name of JAVA_HOME
and finally the change is made with the method SetEnvironmentVariable()
.
Sources:
Windows Powershell Tip of the Week
Environment . net class
Get-Wmiobject
Just an addendum, I had to take the 'lib' from the end of this line here [Environment]::Setenvironmentvariable($javaHome, $itensValidos[$selected]. Installlocation +"lib" , 'Machine') to work properly, android studio said it could not find a valid version of JVM, and passing the path to the installation folder, example: 'C: Program Files Java jdk1.7.0_55' worked
– Alexandre Werner
@Alexandre Werner, thanks for the fix I’ll edit later.
– rray