Passing an entire file to JVM arguments
Asked Answered
D

4

10

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:

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.

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.

Decoupage answered 1/2, 2011 at 18:19 Comment(0)
W
9

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.

Whoops answered 1/2, 2011 at 18:32 Comment(1)
@doug: it will not. Under windows, you need something like for /S delim=..., can't remember, but windows does provide a way to read a file line by line; you add these lines to an environment variable.Whoops
D
8

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

Dordogne answered 1/2, 2011 at 18:33 Comment(1)
I don't think this will work because both the web application and the log4j application need access to the same variables...Decoupage
I
3

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.

Isolecithal answered 1/2, 2011 at 18:31 Comment(1)
getClass().getResourceAsStream() is something you do inside the app after it's started, right? I need this to happen before any application gets started..Decoupage
P
0

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

Presentationism answered 14/5, 2013 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.