How do I compile for compatibility with older versions of Java?
Question ID : 57
Created on 2009-08-31 at 6:00 PM
Author : Veryant Support [email@example.com]
Online URL : http://support.veryant.com/support/phpkb/question.php?ID=57
When I run my program with an older version of the Java runtime environment I get the following error:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
What can I do to resolve this?
How can I see the version number in the .class file?
You can use the -jo compiler command line option in order to specify options to be passed to the Java compiler such as:
"-source" and "-target".
For example, to compile for compatibility with Java 1.4.2 add the following to your compile command line
-jo="-source 1.4 -target 1.4"
To compile a program named MYPROG.cbl:
iscc -jo="-source 1.4" MYPROG.cbl
(you can omit "-target 1.4" since it is the default when specifying "-source 1.4")
Here is the documentation for the -source and -target options from Java
Specifies the version of source code accepted. The following values for release are allowed:
- The compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
- The compiler accepts code containing assertions, which were introduced in JDK 1.4.
- The compiler accepts code containing generics and other language features introduced in JDK 5.
- Synonym for 1.5.
- This is the default value if you are compiling with JDK 6. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
- Synonym for 1.6.
- This is the default value. The compiler accepts code with features introduced in Java SE 7.
- Synonym for 1.7.
Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6).
The default for -target depends on the value of -source:
- If -source is not specified, the value of -target is 1.6
- If -source is 1.2, the value of -target is 1.4
- If -source is 1.3, the value of -target is 1.4
- For all other values of -source, the value of -target is the value of -source.
In order to check the version number in a .class file you can use the javap utility that is distributed with the JDK. For example, the following command will report the version of a program named MYPROG.
javap -verbose MYPROG | grep major
or run "javap -verbose MYPROG" and the version numbers will be reported in the first 5 lines.
Note that MYPROG.class must be in the class path. You can specify a class path with javap -cp.
The major version number will be 48 for Java 1.4.2, 49 for Java 5, 50 for Java 6, and 51 for Java 7.
Back to Original Question