从Windows批处理中读取文件中的环境变量(cmd.exe)

时间:2021-11-20 23:01:48

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:

我正在尝试从批处理文件中读取变量,以便以后在批处理脚本中使用,这是一个Java启动程序。理想情况下,我希望在所有平台(Unix,Windows)上使用相同的设置文件格式,并且也是有效的Java Properties文件。也就是说,它应该是这样的:

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:

是否有可能像在Unix shell脚本中那样读取这些值?可能应该看起来像这样:

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.

我知道这可能是SET setting1 = Value1,但我真的宁愿在所有平台上使用相同的文件格式。

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.

澄清一下:我需要在命令行/批处理环境中执行此操作,因为我还需要设置无法在JVM中更改的参数,如-Xmx或-classpath。

4 个解决方案

#1


15  

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.

这将读取包含“SETTING1 = VALUE1”等字符串的文本文件,并调用SET将其设置为环境变量。

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

setlocal / endlocal用于将环境变量的范围限制为批处理文件的执行。

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

CMD命令处理器实际上非常强大,但具有相当的拜占庭语法。

#2


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.

您可以将属性文件作为参数传递给Java程序(稍后可能会启动主程序)。然后从多平台范例中受益。

#3


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

将通过调用调用:parsePropertiesFile props.properties setting1 setting2来设置变量setting1和setting2

#4


0  

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

您还可以从Java程序中访问OS的环境变量:

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));
        }
    }
}

#1


15  

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.

这将读取包含“SETTING1 = VALUE1”等字符串的文本文件,并调用SET将其设置为环境变量。

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

setlocal / endlocal用于将环境变量的范围限制为批处理文件的执行。

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

CMD命令处理器实际上非常强大,但具有相当的拜占庭语法。

#2


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.

您可以将属性文件作为参数传递给Java程序(稍后可能会启动主程序)。然后从多平台范例中受益。

#3


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

将通过调用调用:parsePropertiesFile props.properties setting1 setting2来设置变量setting1和setting2

#4


0  

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

您还可以从Java程序中访问OS的环境变量:

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));
        }
    }
}