Passing an entire file to JVM arguments

浪子不回头ぞ 提交于 2019-12-03 03:30:15

That's roughly how we do it:

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.

I don't think you can do that via the command line (without some bash hacks, perhaps), but you definitely can do that programatically:

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.

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).

Brian Agnew

Some options:

  1. Wrap your properties in your .jar file and then get your processes to read that properties file from getClass().getResourceAsStream()
  2. Write a batch file to execute your Java processes, and either explicitly list the -D options, or create the command line dynamically.

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.

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.

There is a StackOverflow thread here which describes how to determine the OS from ant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!