views:

552

answers:

2

How can I setup the JVM to automatically load a .properties file in .jar on the classpath on JVM startup? I do not want to configure the properties on the commandline (with -D) but have them in a .properties file.

Is there a way to configure this with the help of Maven?

+4  A: 

No, it's not possible to have the system properties loaded from a file automatically. What you can do is have some sort of launcher that reads a file and automatically "translates" it to -D command line options.

Java WebStart does something like that, with system properties defined in the JNLP file - maybe you can use that.

Michael Borgwardt
+2  A: 

If you are using Maven to package your application, consider using the appassembler-maven-plugin's generate-daemons goal. This will generate JSW based daemon wrappers for Windows and linux. So the bat/sh file used to launch the application will have those properties defined, while still allowing you to specify additional properties via the command line.

You can specify in the execution the defaultJVMSettings property so that the JVM will be launched with those properties. The example below shows how these settings can be defined:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>appassembler-maven-plugin</artifactId>
  <version>1.0</version>
  <execution>
    <id>generate-jsw-scripts</id>
    <phase>package</phase>
    <goals>
      <goal>generate-daemons</goal>
    </goals>
    <configuration>
      <defaultJvmSettings>
        <initialMemorySize>256M</initialMemorySize>
        <maxMemorySize>1024M</maxMemorySize>
        <systemProperties>
          <systemProperty>java.security.policy=conf/policy.all</systemProperty>
          <systemProperty>com.sun.management.jmxremote</systemProperty>
          <systemProperty>com.sun.management.jmxremote.port=8999</systemProperty>
          <systemProperty>
            com.sun.management.jmxremote.authenticate=false
          </systemProperty>
          <systemProperty>com.sun.management.jmxremote.ssl=false</systemProperty>
        </systemProperties>
        <extraArguments>
          <extraArgument>-server</extraArgument>
        </extraArguments>
      </defaultJvmSettings>
      <daemons>
        <daemon>
          <id>myApp</id>
          <mainClass>name.seller.rich.MainClass</mainClass>
          <commandLineArguments>
            <commandLineArgument>start</commandLineArgument>
          </commandLineArguments>
          <platforms>
            <platform>windows</platform>
            <platform>unix</platform>
          </platforms>
        </daemon>
      </daemons>
      <target>${project.build.directory}/appassembler</target>
    </configuration>
  </execution>
</plugin>
Rich Seller
That's a great solution for a standalone app, but in my case I have a library. And I want to bundle the properties with this lib in a way, that every time it is used, the bundled properties are set. So basically I would have to do it in the source code.
desolat