24

I'm trying to read variables from a batch file for later use in the batch script, which is a Java launcher. I'd ideally like to have the same format for the settings file on all platforms (Unix, Windows), and also be a valid Java Properties file. That is, it should look like this:

setting1=Value1
setting2=Value2
...

Is it possible to read such values like you would in a Unix shell script? The could should look something like this:

READ settingsfile.xy
java -Dsetting1=%setting1% ...

I know that this is probably possible with SET setting1=Value1, but I'd really rather have the same file format for the settings on all platforms.

To clarify: I need to do this in the command line/batch environment as I also need to set parameters that cannot be altered from within the JVM, like -Xmx or -classpath.

6 Answers 6

45

You can do this in a batch file as follows:

setlocal
FOR /F "tokens=*" %%i in ('type Settings.txt') do SET %%i
java -Dsetting1=%setting1% ...
endlocal

This reads a text file containing strings like "SETTING1=VALUE1" and calls SET to set them as environment variables.

setlocal/endlocal are used to limit the scope of the environment variables to the execution of your batch file.

The CMD Command Processor is actually quite powerful, though with a rather byzantine syntax.

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

4 Comments

After a long time looking for a solution, I found this solution. Thank you!
How would you ignore a "comment" (for example, a line starting with a "#" character) - ?
@malthe - one way is just to ignore any error messages the comment lines produce, or hide them thus FOR /F "tokens=*" %%i in ('type Settings.txt') do SET %%i 2> NUL:
@malthe - Add eol=<comment character>. FOR /F "tokens=* eol=#" %%i in ('type Settings.txt') do SET %%i
4

This one-liner also unquotes value before assigning it to a variable:

for /F "delims== tokens=1,* eol=#" %%i in (%filename%) do set %%i=%%~j
  • delims== split lines by = character
  • tokens=1,* put first token (name) into %%i and the rest (value) into %%j
  • eol=# skip lines starting with # (comments)
  • %%~j remove surrounding quotes from value

Comments

3
FOR /F "eol=# tokens=*" %%i IN (%~dp0.env) DO SET %%i

It will only work in .bat file (not cmd.exe)

This will do the following:

  • Skips all lines that start with # (can be used for comments)
  • Reads the file .env which sits in the same directory of the batch script and pass the lines to SET

To be more helpful This is the .env file i was parsing

# The name of the mariadb service to be installed
MariaDb_serviceName=Swoole_MariaDB
# which port the mariadb server will run at
MariaDb_port=1214
# password of mariadb root user
MariaDb_rootPassword=
# Database to be created after installing mariadb
MariaDb_createDataBase=data
# the server service name to be installed
Server_serviceName=Swoole_Server

2 Comments

No need for /a, as batch knows about only one variable type anyway: STRING. set /a is used to translate strings into integers to do arithmetic and assigns again a string to the variable.
@Stephan I have modified the answer, I did not know about it thank you!
2

You can pass the property file as a parameter to a Java program (that may launch the main program later on). And then benefit from the multi platform paradigm.

2 Comments

Yes, that's generally true, but I need to configure stuff like -Xmx too, which of course cannot be changed once the JVM is running.
Oh I see, it's sounds overkill compare to the solutions but you may re-launch the Java from another Java. Don't get my wrong I do prefer the selected solution.
1

It may be wise to only import specific variables from a properties file (ones you know about ahead of time), in that case I recommend a function like the following:

:parsePropertiesFile
    set PROPS_FILE=%1
    shift
    :propLoop
    if "%1"=="" goto:eof
    FOR /F "tokens=*" %%i in ('type %PROPS_FILE% ^| findStr.exe "%1="') do SET %%i
    shift
    GOTO propLoop
goto:eof

Which would be called by call:parsePropertiesFile props.properties setting1 setting2 to set the variables setting1 and setting2

Comments

0

You can also access the OS' environment variables from within a Java program:

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }
    }
}

Comments

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.