1

A way to run multiple programs in a bat file.

Objective

Distribute a software with GDAL as a dependency.

To do that I have downloaded gdal binaries from GIS Internals. The downloaded data has a .bat file to set environment variables. It uses set command to set the environment variables.

As per my limited knowledge in windows bash scripting, I understand that the environment variables set by set are limited to current command prompt itself and are reset when a new command prompt is launched.

Also, is it true that a calling a batch file from a batch file launches new command prompt which when closes doesn't affect the next command called in the parent script.

There is another issue here - will the environment variables affect the process created (such as calling gdal_translate) by a Java program? If it doesn't, then there is no point in setting local environment variable.


Final Requirement:

How to use the environment variables set in another batch file (called from a batch file) in the next line of the parent batch file, without using setx?

Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64

1 Answers1

4

If you use the CALL command to run the batch file as specified in the accepted answer on the question you linked to, then the environment variables will pass up to the parent batch file.

There are three times where this is not true:

  • When using SETX neither the parent nor child batch file will get the environment variables.
  • When using SETLOCAL and ENDLOCAL inside the child batch file the parent batch file will not get the environment variables.
  • When running the child batch file using start or cmd.exe /c.

So long as you run your Java application in the same environment (i.e. the same batch file), it will pick up the same environment variables. You can verify this with an application like Process Explorer.

Running set _kjhgkjshdgkjhdfg=TEST before running a .jar file resulted in the screenshot below using any of:

  • javaw -jar jarfile.jar
  • cmd /c javaw -jar jarfile.jar
  • start cmd /c javaw -jar jarfile.jar

Process Explorer

Worthwelle
  • 1,244
  • 1
  • 16
  • 19
  • What about a Java program which calls a program using processbuilder? Can that program access the variables set using `SET`? – Tarun Maganti Jun 07 '19 at 16:22
  • Is there a way to verify the environment? – Tarun Maganti Jun 07 '19 at 16:24
  • Also, I didn't use the call command. There were some problems with it. I used `start \f CMD \c javaw -jar jarfile.jar` and I remember it not picking up the values. I'll try to put the relevant screenshots if you want by recreating the scenario tomorrow. I don't have access to any system now. – Tarun Maganti Jun 07 '19 at 16:27
  • I'd have to verify before accepting your answer here. But thanks. – Tarun Maganti Jun 07 '19 at 16:28
  • Can you please post that program as a post script to your answer. – Tarun Maganti Jun 07 '19 at 16:38
  • What about that `set` which ran in another bat file which was invoked using `start CMD \c abc.bat` – Tarun Maganti Jun 07 '19 at 16:40
  • You seem to have changed the answer. Which helps me understand but doesn't solve the problem. I'll try something else. Thank you. – Tarun Maganti Jun 07 '19 at 16:50
  • What is the problem you're trying to solve? If you're having issues getting `CALL` to work, then you need to add that to your question. – Worthwelle Jun 07 '19 at 16:53
  • I have Java code calling `gdal_translate`. To make the code portable and avoid installation in user system, I downloaded gdal binaries, which contained a script to set environment variables. So, I created a bat file which first called that script and launched my program. When that program ran `gdal_translate`, it failed that some DLL file is missing. That bat file to initialise has `set`, I changed it to `setx`, ran that script once and launched the Java program which worked fine. But, I don't want to ask users to permanently set their environmental variables, as it might get messy. – Tarun Maganti Jun 07 '19 at 16:58
  • it might get messy when they change directories and run that script again. And I heard that environmental variables have size limit and they get truncated. – Tarun Maganti Jun 07 '19 at 17:00
  • What I'm thinking now is run the Java program from that script itself so that those variables will be picked up as you mentioned in you other answer. – Tarun Maganti Jun 07 '19 at 17:02
  • 1
    This sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What you're asking in this question is only tangentially related to the actual problem. I suggest creating a new question and describing the actual problem, which is a missing DLL file and/or an issue with an existing batch file. In the new question post the batch file as part of the question so people have context for answering. – Worthwelle Jun 07 '19 at 17:06
  • Missing DLL was solved when I ran the script using `setx` and then separately running the program. But, as you said, I shall write a new question. – Tarun Maganti Jun 07 '19 at 17:12
  • I agree, it is an XY problem. – Tarun Maganti Jun 07 '19 at 17:14
  • What is the software you used to check the variables picked up by the java program, in the screenshot? – Tarun Maganti Jun 10 '19 at 10:27
  • There is a problem with using `Call` to start a bat file. The bat file runs and exits the operation, the steps specified after `Call` in the parent bat file are not executed. – Tarun Maganti Jun 10 '19 at 11:27
  • The program is Process Explorer, as linked in the answer. Are you using `EXIT` in the child batch file? Is there an error? `CALL` is specifically used to allow the execution to continue. – Worthwelle Jun 10 '19 at 14:50