When compiling a Java source file using javac from JDK 7 the generated class file format is incompatible with previous JRE versions including version 6.
Therefore when trying to run the class file using Java 6 the following cryptic exception is thrown by Java:
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld: Unsupported major.minor version 51.0
Class version 51.0 is the version of class file generated by javac 7 while Java 6 expects class file format 50.0 or lower.
To solve the problem you need to tell javac 7 to create a class format compatible with previous versions of Java.
With previous versions of java adding the -target flag solved the problem:
javac -target 1.6 HelloWorld.java
However as of Javac 7 this command emits the following error:
javac: target release 1.6 conflicts with default source release 1.7
Adding the -source flag fixes this problem but generates a warning:
javac -source 1.6 -target 1.6 HelloWorld.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
javac is warning us that we are compiling Java source code according to Java 6 source format but linking it with the Java runtime libraries of Java 7.
This works but may lead to subtle runtime bugs.
To fix the warning:
1. Install a Java 6 JRE.
2. Add the -bootclasspath flag to point to the Java 6 rt.jar from the javac 7 command line:
javac -bootclasspath <JRE 1.6 home>\lib\rt.jar -source 1.6 -target 1.6 HelloWorld.java
You should now see that the source compiles without warning and the resulting class file is executable using Java 6.
If you received a class file or a Jar file from a 3rd party and would like to see which class format version is being used, you can use a tool like the jclasslib bytecode viewer
Another concern is that if you are redistributing the JDK together with your product, in this case you'll have to distribute both the JDK 7 and JRE 6 in order for your customers to be able to compile Java 6 compatible class files using a Java 7 javac.