Skip to content
⌘ NSIS Forum Archive

Batch file not recognizing the environment variable

5 posts

sandeepsirsgi#

Batch file not recognizing the environment variable

Hi,

I am able to set the environment variable by using following following NSIS script.

Push JAVA_HOME
Push "d:\JDK1.5"
Call WriteEnvStr

After that i am running a batchfile using NSIS which is refering the JAVA_HOME to execute the batch.
But the .bat file is not recognizing the environment variable JAVA_HOME.
If i run that .bat file through command prompt then it's working.

Can any one help me on that.

Regards
sandy
kichik#
Setting environment variables this way doesn't affect running processes. Use SetEnvironmentVariable for that.
sandeepsirsgi#
I am running a batch file which is refering to JAVA_HOME and
taking APP_HOME as classpath. I am able to set environment variable by using following NSIS script.

Push JAVA_HOME
Push "d:\JDK1.5"
Call WriteEnvStr
Push APP_HOME
Push "d:\application"
Call WriteEnvStr

ReadEnvStr $R0 "PATH"
StrCpy $R0 "$R0;$TEMP2;$TEMP1"
SetEnv::SetEnvVar "PATH" "$R0"
Exec batchfile.bat

Here $TEMP1 and $TEMP2 contains path to java_home
and batch file respectively. When batch file is executed
it's not able to recognize

Still not able to execute batch file.
Red Wine#edited
Case 1.
If you want to set an environment variable only for the installer process and its sub-processes use:
!define JAVA_HOME "d:\JDK1.5"
!define APP_HOME "d:\application"

Section "Add Env Var"
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
StrCpy $R0 "$R0;${JAVA_HOME};${APP_HOME}"
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("PATH", R0).r2'
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
exec 'batchfile.bat'
SectionEnd
Case 2.
If you want to set an environment variable that will stick for every other process and after reboots too...
!define JAVA_HOME "d:\JDK1.5"
!define APP_HOME "d:\application"

!include WriteEnvStr.nsh

Section "Add Env Var"
!ifdef ALL_USERS
!define ReadEnvStr_RegKey \
'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
!else
!define ReadEnvStr_RegKey 'HKCU "Environment"'
!endif

Push JAVA_HOME
Push '${JAVA_HOME}'
Call WriteEnvStr
Push APP_HOME
Push '${APP_HOME}'
Call WriteEnvStr

ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
;ensure that is written valid for NT only
ReadRegStr $0 ${ReadEnvStr_RegKey} 'JAVA_HOME'
ReadRegStr $1 ${ReadEnvStr_RegKey} 'APP_HOME'
StrCpy $R0 "$R0;$0;$1"
;or just this
;StrCpy $R0 "$R0;${JAVA_HOME};${APP_HOME}"
System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("PATH", R0).r2'
ReadEnvStr $R0 "PATH"
messagebox mb_ok '$R0'
writeuninstaller '$EXEDIR\uninst.exe'
exec 'batchfile.bat'
SectionEnd
# ...
Section uninstall
# remove the variable
Push JAVA_HOME
Call un.DeleteEnvStr
Push APP_HOME
Call un.DeleteEnvStr
SectionEnd