0

I do a thing to install JDK when a Windows virtual machine boot, use a cloudinit user-data to transfer a PowerShell script to the windows machine, and run the script to install JDK.

$softwares = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
$jdk = $softwares | Where-Object DisplayName -match 'Java SE Development Kit'
$java_home = $jdk.InstallLocation.Trim('\')
#$java_home = "C:\Program Files\Java\jdk1.7.0_80"
$classpath = ".;%JAVA_HOME%\lib;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar"
$path = ";%JAVA_HOME%\bin;" + $env:Path
[Environment]::SetEnvironmentVariable("JAVA_HOME", $java_home, "machine")
[Environment]::SetEnvironmentVariable("CLASSPATH", $classpath, "machine")
[Environment]::SetEnvironmentVariable("PATH", $path , "machine")

The problem is my script installs the JDK successfully and modifies the environment variables correctly, but the command java still doesn't run. The path is right and the path in the registry is right,too.

I am sure the path is right

I am sure the path is right because when I modify "Path" (delete ',' in the head of "Path") by steps such as MyPC/RigthClick/Properties/Advaced/EnvironmentVariables/. And I also try to configure "Path" without ";" in the head, still can't run java successfully, modify "Path" by add ";", it runs well.

5
  • Just to be sure: did you run java from a command prompt, and if yes, did you open a new one after modifying the Path? Commented Aug 10, 2017 at 7:42
  • Yes,I have tried to open a new one ,and I also tried to reboot the windows VM. Commented Aug 10, 2017 at 9:40
  • Check if there's java in %JAVA_HOME%/bin/. Try the command where java.Your path shouldn't start with ;. Try to echo %PATH% after your operations and check it. Commented Aug 10, 2017 at 9:59
  • Goddamn, I sohuld've post it as an answer :p Commented Aug 11, 2017 at 7:40
  • I make a mistake, I mean the Ansgar Wiechers's answer is right , sorry for that. Commented Aug 11, 2017 at 9:57

1 Answer 1

0

Environment variables are stored in the registry. [Environment]::SetEnvironmentVariable() writes the given variable as a REG_SZ value, but for nested variables to be expanded you need a REG_EXPAND_SZ value.

If you want to use %JAVA_HOME%\bin in the PATH use Set-ItemProperty instead of [Environment]::SetEnvironmentVariable():

$regkey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
Set-ItemProperty -Path $regkey -Name 'Path' -Value $path -Type ExpandString

Beware that there may be other pitfalls when using nested variables in environment variables due to the order in which they're expanded. Raymond Chen described the behavior in the article Windows Confidential: The hidden variables.

Sign up to request clarification or add additional context in comments.

3 Comments

There's a typo in Set-ItemProeprty and The hdden variables :3
@Nathan True. Thanks for the heads up. Fixed.
It's fixed, thank you all very much! I would read the article carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.