views:

68

answers:

3

I'd like to be able to set a property on the JVM using the -D switch. If I do that, can I access it from my code? If yes, how?

+10  A: 

Use System.getProperty("mykey");

McDowell
can `mykey` be any string?
Geo
Try it out and check the link
Filip Ekberg
@Geo, yes `mykey` can be any string. However, there are a number of properties already defined by default. There's a list of them in the Java documentation at this page. http://java.sun.com/javase/6/docs/api/java/lang/System.html
Glen
+3  A: 

Apart from System.getProperty, there's also Integer.getInteger and Boolean.getBoolean, if you want to get an integral or boolean value instead. :-)

Chris Jester-Young
+1  A: 

and as a bonus:

System.setProperty("yourkey",yourValue); //works very well too

allows you to make sure the property is set if you want to test things

Peter