0

I have a web application running in tomcat as a service, When I call a batch file through API where the batch file contains ,

@echo off 
setx /M test_path "C:\test"
echo "path set "

And I have another another batch file which is called thorugh another API , it contains

@echo off
echo test_path :: %test_path% > test.log
echo "Got the path"

The %test_path% is not visible to second batch file. I m trying to make it possible without restarting tomcat service .

I need to know whether is it possible ,

Kamikaze
  • 23
  • 1
  • 8
  • see http://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w Does it have to be an environment variable? – Richard Jan 02 '17 at 19:27
  • @Richard I have tried those methods , Even if I try to echo the variable which was set through a tomcat java web application as an user by opening a new command prompt , it is not showig up. But If I open and close the enviromental variables window (sysdm.cpl,EditEnvironmentVariables) ,then I am able to echo it in user command prompt. I don't know what happens when I open and close the window . – Kamikaze Jan 03 '17 at 06:12
  • There doesn't appear to be a relationship between the processes that will be started when the first and second request arrive. Setting an environment variable in process A (which will terminate) will have no bearing on the parent process (the API service). So making a later request to some other API will launch a new process (B) to run the batch script, the environment will not have anything in it from process A. – Christopher Schultz Jan 03 '17 at 22:02
  • That would be true if you used set. setx sets persistent system-wide environment variables – Richard Jan 04 '17 at 13:00

1 Answers1

0

You can read it directly from the registry.

for /F "tokens=*" %%G IN ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "test_path"^|find "    REG_SZ    "') DO (set testpath=%%G)
set testpath=%testpath:~23%
echo %testpath%

If you take this approach, it would be better to set a registry value directly under a key for your application.

Richard
  • 1,121
  • 1
  • 9
  • 15
  • @Ricchard , I have done this but only as a work around .But its not the right way to do it as we are teporarily setting it . The variable set should be visible to user as well as my app in tomcat service but using this method , the variable is only visible to my app not the user. I need it to be visible to the user – Kamikaze Jan 04 '17 at 10:31
  • There doesn't seem to be a reliable way of doing this without a restart, unfortunately. – Richard Jan 04 '17 at 13:02
  • I have found a way . you can use [paexec.exe](https://www.poweradmin.com/paexec/) to set the enviromental variable( system/user) to make it visible over cmd(user). And for your application , just script out the variables from registry and set them temporarly . – Kamikaze Jan 16 '17 at 13:39