Determine a the JDK version used to make a JAR file

April 25, 2008

Maybe there’s already a tool to do this, but a quick Google search only yield this tip.  So, I made a quick little JAR reader to fetch the estimated version from the manifest:

import java.util.*;
import java.util.jar.*;
import java.io.*;

/*
 * Sends the "estimated" JDK version (expected to be in the jar manifest "Created-By" attribute)
 * to System.out.
 */
public class JarJDKVersion {

	public static void main(String[] args) {

		JarFile jarFile = null;
		Manifest manifest = null;
		String version = null;
		
		// Report usage if no parameters given
		if (args.length < 1) {
			System.out.println("usage: java JarJDKVersion ");
			System.exit(0);
		}
		
		// Open specified jar file
		try {
			jarFile = new JarFile(args[0]);
		} catch (IOException e) {
			System.err.println("Unable to read jar file: " + args[0]);
			System.exit(1);
		}
		
		// Fetch manifest from jar file
		try {
			manifest = jarFile.getManifest();
		} catch (IOException e) {
			System.err.println("Unable to read manifest from jar file: " + args[0]);
			System.exit(2);
		}
		
		// Display version from jar manifest
		Attributes attributes = manifest.getMainAttributes();
		version = attributes.getValue("Created-By").toString();
		if (version != null)
			System.out.println(version);
		else {
			System.err.println("Unable to determine version from jar manifest.");
			System.exit(3);
		}
		
	}

}

Download the source code or the class file.