A Perl utility to extract Java version information from compiled Java class files by analyzing their bytecode headers.
JavaInfo reads Java .class files and identifies the Java version used to compile them. It works by examining the bytecode header of each class file, specifically the magic number (0xCAFEBABE) and the major/minor version numbers that indicate the Java compiler version.
The tool can process individual files or recursively scan entire directories, making it useful for:
- Auditing Java dependencies and libraries
- Verifying compilation targets in build artifacts
- Identifying version mismatches in projects
- Understanding compatibility requirements
- Perl 5.x or later
- Standard Perl modules (included by default):
File::Spec::Functions
- Clone or download this repository
- Make the script executable:
chmod +x JavaInfo
- Optionally, add it to your PATH:
sudo cp JavaInfo /usr/local/bin/
./JavaInfo <file or directory> [<file or directory> ...]Analyze a single class file:
./JavaInfo MyClass.classScan a directory recursively:
./JavaInfo /path/to/project/target/classesProcess multiple paths:
./JavaInfo src/main/java build/classes lib/*.jarMyClass.class: Java 11 [0/55]
Utils.class: Java 8 [0/52]
LegacyCode.class: Java 1.4.2 [0/48]
The output format is:
<filename>: Java <version> [<minor>/<major>]
Where:
<filename>is the path to the class file<version>is the Java version (e.g., 8, 11, 17, or 1.4.2 for older versions)<minor>/<major>are the bytecode version numbers
The tool uses the following mapping between bytecode major versions and Java releases:
| Major Version | Minor Version | Java Version |
|---|---|---|
| 45 | 0-3 | 1.0.2 |
| 45 | 4+ | 1.1.8 |
| 46 | - | 1.2.2 |
| 47 | - | 1.3.1 |
| 48 | - | 1.4.2 |
| 49 | - | 5 |
| 50 | - | 6 |
| 51 | - | 7 |
| 52 | - | 8 |
| ... | - | ... |
For Java 5 and later, the formula is: Java Version = Major Version - 44
The script will display error messages and exit if:
- A file cannot be opened or read
- A directory cannot be accessed
- A file is not a valid Java class file (missing the
0xCAFEBABEmagic number)
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
See LICENSE file for details.
For security concerns, please see SECURITY.md.
This project adheres to a code of conduct. See CODE_OF_CONDUCT.md for details.