How to keep SDK and JRE Java installed and updated in a windows development environment

Asked

Viewed 176 times

2

How to maintain a secure, clean and up-to-date java installation (SDK and JRE) of Java on a computer used for software development?

I’m having trouble maintaining my development environment because I have Eclipse and Android Studio IDE installed, as well as some other applications that use java, and today I had a huge problem after a Java update, the path in the Windows variables started pointing to folders that were not the correct ones, and then Eclipse and Android Studio stopped working, then I adjusted it and Tomcat got lost.

1 answer

1


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')

inserir a descrição da imagem aqui

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, thanks for the fix I’ll edit later.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.