java 将整个文件传递给 JVM 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4866388/
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
Passing an entire file to JVM arguments
提问by doug
I have several systems that all need to load the same properties to the JVM. I can use the -D flag to load one property at a time, but i am looking for something that will load all the properties in an entire file at one time. For instance:
我有几个系统都需要将相同的属性加载到 JVM。我可以使用 -D 标志一次加载一个属性,但我正在寻找可以一次加载整个文件中所有属性的东西。例如:
I could just add --options-file=blah.properties to all jvms on my network, once, and from then on only change the properties file, which could be a single central file over a network share.
我可以将 --options-file=blah.properties 添加到我网络上的所有 jvm,一次,然后只更改属性文件,它可以是网络共享上的单个中央文件。
Thank you,
谢谢,
EDIT: Any arguments or commands must also work in a windows environment. Therefore any bash or scripting hacks specific to unix will not work.
编辑:任何参数或命令也必须在 Windows 环境中工作。因此,任何特定于 unix 的 bash 或脚本黑客都将不起作用。
采纳答案by 9000
That's roughly how we do it:
我们大致是这样操作的:
java $(tr '\n' ' ' < options_file) other args...
java $(tr '\n' ' ' < options_file) other args...
Here options_file
contains ready -Dsomething
or -Xsomething
values, one per line. The tr
command just replaces every newline with a space.
这里options_file
包含就绪-Dsomething
或-Xsomething
值,每行一个。该tr
命令只是用空格替换每个换行符。
回答by Costi Ciudatu
I don't think you can do that via the command line (without some bash hacks, perhaps), but you definitely can do that programatically:
我不认为你可以通过命令行来做到这一点(也许没有一些 bash 黑客),但你绝对可以通过编程来做到这一点:
Simply set one property -DmyPropertiesFile=/your/properties/file.properties
and then read that with one of the Properties.load()
overloads. After that, System.setProperties(yourProps)
should do what you expect.
只需设置一个属性-DmyPropertiesFile=/your/properties/file.properties
,然后使用Properties.load()
重载之一读取它。之后,System.setProperties(yourProps)
应该做你所期望的。
Of course, this requires that you can hook this code early enough so that your properties will be available when needed (For instance, if the main()
method is yours, this is perfect).
当然,这要求您可以足够早地挂钩此代码,以便您的属性在需要时可用(例如,如果该main()
方法是您的,则这是完美的)。
回答by Brian Agnew
Some options:
一些选项:
- Wrap your properties in your .jar file and then get your processes to read that properties file from
getClass().getResourceAsStream()
- Write a batch file to execute your Java processes, and either explicitly list the -D options, or create the command line dynamically.
- 将您的属性包装在 .jar 文件中,然后让您的进程从中读取该属性文件
getClass().getResourceAsStream()
- 编写一个批处理文件来执行您的 Java 进程,或者明确列出 -D 选项,或者动态创建命令行。
I solve this problem normally by using Spring (used for other reasons too) and a PropertyPlaceholderConfigurer. That allows me to specify one or more locations for property files and modifies the Spring configuration in-place.
我通常通过使用 Spring(也用于其他原因)和一个PropertyPlaceholderConfigurer来解决这个问题。这允许我为属性文件指定一个或多个位置并就地修改 Spring 配置。
回答by romeara
If you use Ant to lanuch the Java process, 9000's answer (plus his windows comment) will work, and you can have the launcher deal with the OS difference.
如果您使用 Ant 来启动 Java 进程,9000 的答案(加上他的 windows 评论)将起作用,并且您可以让启动器处理操作系统差异。
There is a StackOverflow thread herewhich describes how to determine the OS from ant
这里有一个 StackOverflow 线程,它描述了如何从 ant 确定操作系统