Related

3 Comments

You forgot to mention that you can set system properties at JVM invocation.

You also didn’t mention that your example has problems of its own – determining the JVM version is trivial. Acting on it can be difficult. The only way to make sure that your program runs on a certain (older) version of Java is to compile and test it with that JVM. But then, the part for the newer-version JVM might not compile anymore. And if you compile it all with the newer version, the “old way” might accidentally call methods/classes that aren’t there.

A *useful* example – something many don’t do but should – would be grabbing the line.separator property instead of adding “\n” to try to force a line break, because the different platforms have different line separators (\n for Linux, \r\n for Windows and \r for Macs IIRC).