Bytecode Version Inspector:
bcver Ant Task

Description

Bytecode Version Inspector examines class and jar files for bytecode version information. The tool will traverse a directory tree, and can report on the individual bytecode version of each class file and the range of bytecode versions found in each directory or jar file. The tool is also available as a command-line application.

The task can be configured to produce a report, either to the console or to a file. The report can be generated in a standard format, either plain (ASCII text) or XML, or in a custom format.

The task can be configured to fail the build if it encounters a bytecode version greater than or less than a specific value. The build will fail only after any reports have been generated.

Bytecode Versions

Java bytecode is the encoding used for class files, as output by a Java compiler. It is versioned separately from the Java language. The following table gives the bytecode versions that will run on a particular JVM version, as listed by Sun or determined by experiment.

Bytecode Version JVM Version Source
45.0 - 45.3 1.0.2 Sun
45.0 - 45.65535 1.1.* Sun
45.0 - 46.0 1.2 Sun; experiment suggests that some 1.2 JVMs also support 46.3
45.0 - 47.0 1.3 Experiment
45.0 - 48.0 1.4 Experiment
45.0 - 49.0 5 (1.5) Experiment

With this information, you can inspect your files to make sure they will run with a particular JVM. For example, if you want a particular jar file to run with any JVM from version 1.3 or later, you would inspect that jar file to make sure all bytecode versions for any class files in the jar lie in the range [45.0 .. 47.0].

Parameters

Attribute Description Required
checkdir "true"/"yes" to check subdirectories; defaults to "true". No.
checkjar "true"/"yes" to check inside jar files; defaults to "true". No.
maxversion fail the build if any class file is found with a version greater than that specified. No.
minversion fail the build if any class file is found with a version less than that specified. No.

Nested Elements

fileset

The bcver task supports any number of nested <fileset> elements to specify the files to be checked for version information.

formatter

The version reports can be printed in different formats. Output is sent to a file, whose name is set by the file attribute of the <formatter>. One bcver task can support any number of formatters. If there are no formatters, then no report is produced.

There are two predefined formatters—one prints the results in XML format, the other emits plain text. The formatter named plain prints the results as ASCII text, while the formatter named xml writes an XML representation. The report is sent to standard output, unless a file is specified. Custom formatters, which must implement com.bennettconsulting.bcver.ResultFormatter, can be specified.

Attribute Description Required
type Use a predefined formatter (either xml or plain). Exactly one of these.
classname Name of a custom formatter class.
file Name of file to write output to. No; defaults to standard output.
classinfo "true"/"yes" to include version information for individual classes. Default depends on formatter; defaults to true for the standard formatters. No.
dirsummary "true"/"yes" to include a summary of the versions found in a directory. Default depends on formatter; defaults to true for the standard formatters. No.
jarsummary "true"/"yes" to include a summary of the versions found in a jar. Default depends on formatter; defaults to true for the standard formatters. No.

Examples

<taskdef resource="bcvertask.properties" classpath="bcver.jar"/>

Establishes the bcver task.

<bcver checkdir="true" checkjar="true">
    <formatter type="xml" file="classinfo.xml"/>
    <fileset dir="dist" includes="**/*"/>
</bcver>

Checks the directory named dist and all subdirectories of it for Java version info in class and jar files, and writes an XML-formatted report to a file named classinfo.xml.

<bcver checkdir="false" maxversion="47.0">
    <fileset dir="${basedir}" includes="*"/>
</bcver>

Checks the base directory, but no subdirectories, for Java bytecode version info, and fails the build if any file is found with a Java bytecode version greater than 47.0 (corresponds to Java 1.3). No report is produced.

License

This tool is distributed under the Apache 2.0 License. The license is also available on the web.