set JAVA_HOME via command line
setx -m JAVA_HOME "C:\Program Files\Java\jre1.8.0_151"
set the PATH as well, keeping in mind that a new cmd window will be required for the changes to take effect. Also, the BIN subdirectory should be added to PATH.
setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";