Java 从 Windows Batch (cmd.exe) 文件中读取环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/232747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read environment variables from file in Windows Batch (cmd.exe)
提问by Martin Probst
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 属性文件。也就是说,它应该是这样的:
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。
采纳答案by Joe
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 命令处理器实际上非常强大,尽管它的语法相当拜占庭。
回答by call me Steve
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 程序(稍后可能会启动主程序)。然后受益于多平台范式。
回答by micro
You can also access the OS' environment variables from within a Java program:
您还可以从 Java 程序中访问操作系统的环境变量:
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));
}
}
}
回答by Jared
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
将调用它call:parsePropertiesFile props.properties setting1 setting2
来设置变量 setting1 和 setting2