How do I fix it?
This error means that the JRE that is being used to execute your class code does not recognise the version of Java used. Usually because the version of Java that generated your class file (i.e. compiled it) is newer.
To fix it, you can either
a) Compile your Java sources with the same, or older, version of the Java compiler as will be used to run it. i.e. install the appropriate JDK.
b) Compile your Java sources with the newer version of the Java compiler but in compatibility mode. i.e. use the -target
parameter.
c) Run your compiled classes in a JRE that is the same, or newer, version as the JDK used to compile the classes.
You can check the versions you are currently using with
javac -version
for the compiler, and java -version
for the runtime.
Should I install the JDK, and setup my PATH variable to the JDK
instead of JRE?
For compilation, certainly, install and configure the specific JDK that you want.
For runtime, you can use the one that comes with the JDK or a standalone JRE, but regardless, make sure that you have installed the right versions and that you have configured your PATH such that there are no surprises.
What is the difference between the PATH variable in JRE or JDK?
The PATH environment variable tells the command shell where to look for the command you type. When you type java
, the command shell interpreter will look through all the locations specified in the PATH
variable, from left to right, to find the appropriate java
runtime executable to run. If you have multiple versions of Java installed - i.e. you have the java
executable in multiple locations specified in the PATH variable, then the first one encountered when going from left to right will be the one that is executed.
The compiler command is javac
and only comes with the JDK. The runtime command is java
and comes with the JDK and is in the JRE.
It is likely that you have one version (51.0 = Java 7) of javac
installed, and you also have the same version of java
installed, but that another previous version of java
is appearing earlier in the PATH and so is being invoked instead of the one you expect.