Rounding up runtime properties

Runtime properties (java -D options) are useful in a development environment to parameterize properties that varies because of workstation and eclipse workspace setup. For example, I have a number of runtime properties to specifiy the database url and super user credentials for database testing:

It becomes problematic when these runtime properties grow and there are multiple runtime environments. For example, in my development environment, I have to maintain the runtime properties for eclipse JDK settings, tomcat JAVA_OPTS, jBoss JAVA_OPTS. Also when there are many of these properties, they become difficult to organize and maintain.

One way around this problem with Java 5 (or greater) is to use the java launcher -javaagent option. This option allows you to intercept the call to static void main() method of you runtime class and one of the many things you could do is to manipulate the runtime environment.

Steps

Here are the steps. Create a jar e.g. FSinstrumentation.jar with a class such as follows:

Conclusion

Now you only have one place (runtime.properties file) to provide all the properties and you can also organize the file with some comments. As you can see from the instrumenter class, you can pass additional arguments to the instrumenter using argentArgs. There are many more things you could do with this mechanism and I suggest exploring the java.lang.instrument.Instrumentation interface.