Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set properties from .properties file in .jar on JVM startup

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?

like image 638
desolat Avatar asked Sep 24 '09 09:09

desolat


2 Answers

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.

like image 54
Michael Borgwardt Avatar answered Oct 13 '22 01:10

Michael Borgwardt


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>
like image 38
Rich Seller Avatar answered Oct 13 '22 01:10

Rich Seller