This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You can compile for an older JVM using -source and -target; that would be -source 1.6 -target 1.6 for Java 6. However, you will not be able to use any new language constructs like try-with-resources or switch statements with Strings. Likewise, if you choose 1.4 or before as the source and target, you won't be able to use generics.
There's one danger with this approach though; it doesn't prevent you from compiling code that uses classes, methods and fields that were added in Java 7, but that code will fail in a Java 6 JVM.
Documentation wrote:Java SE 7 is binary-compatible with Java SE 6 except for the incompatibilities listed below. Except for the noted incompatibilities, class files built with the Java SE 6 compiler will run correctly in Java SE 7.
The class file version for Java SE 7 is 51, as per the JVM Specification, because of the invokedynamic byte code introduced by JSR 292. Version 51 class files produced by the Java SE 7 compiler cannot be used in Java SE 6.