1
votes

Ours is a legacy Java project, which is maintained for more than 15 years. We have more than 800 modules.

From struts to spring, variety of framework have been used. Based upon the need at that time, developers has chosen the framework and done the development. So there is no standardization.

Now we have requirement to upgrade JDK from 1.6 to 1.8 version.

  1. Is there any way to know easily, which frameworks will have a impact?
  2. Whether any information available as API, to get the JDK version of any framework?

Pls let me know know....

2

2 Answers

0
votes

Is there any way to know easily, which frameworks will have a impact?

There is no easy way.

You need to read the documentation for each framework you are using to find out if you need to upgrade it.

Or just try upgrading the Java platform and see what breaks. (As a general rule, things won't break in the Java 6 to Java 8 migration. More stuff breaks in Java 8 to Java 11 ... but that's not what you are doing now.)

Whether any information available as API, to get the JDK version of any framework?

There is no such API.

0
votes

Upgrading will tell you, with some testing. If you have a system of 800+ modules you should consider addressing your standardisation issues before you upgrade - try to use same versions of your dependencies - such as JDBC drivers, logging jars etc, and same for your developer build environments so everyone can make the same step together - and therefore rollback together if it fails at some stage.

This class will dump out versions of all .class and .jar files it sees in any directories or files it scans. It may help. Or not.

public class ShowClassVersions implements FileVisitor<Path> {
    private static final byte[] CLASS_MAGIC = new byte[] {(byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe};
    private static final byte[] bytes = new byte[8];
    private TreeMap<String,ArrayList<Path>> vers = new TreeMap<>();

    ShowClassVersions() {
    }
    private void scan(Path f) throws IOException {
        if (Files.isDirectory(f)) {
            Files.walkFileTree(f, this);
            return;
        }
        record(f);
    }
    private static String versionOfFile(Path f) throws IOException {
        String fn = f.getFileName().toString();
        if (fn.endsWith(".class"))
            return String.valueOf(versionOfClass(f));
        if (fn.endsWith(".jar"))
            return String.valueOf(versionOfJar(f));
        return null;
    }

    public static String versionOfClass(Path p) throws IOException {
        String version = null;
        try(InputStream in = Files.newInputStream(p)) {
            version = versionOfClass(in);
        }
        return version;
    }

    public static String versionOfClass(InputStream in) throws IOException {
        String version = null;
        int c = in.read(bytes);
        if (c == bytes.length && Arrays.mismatch(bytes, CLASS_MAGIC) == CLASS_MAGIC.length) {
            int minorVersion = (bytes[4] << 8) + (bytes[4] << 0);
            int majorVersion = (bytes[6] << 8) + (bytes[7] << 0);
            version = ""+majorVersion + "." + minorVersion;
        }
        return version;
    }
    public static String versionOfJar(Path p) throws IOException {
        String version = null;
        try(ZipInputStream in = new ZipInputStream(Files.newInputStream(p)))  {
            ZipEntry entry = null;
            while (version == null && (entry = in.getNextEntry()) != null) {
                if (entry.getName().endsWith(".class") /* && entry.getSize() > bytes.length */)  {
                    version = versionOfClass(in);
                }
            }
        }
        return version;
    }
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        record(file);
        return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        return FileVisitResult.CONTINUE;
    }
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        return FileVisitResult.CONTINUE;
    }
    void record(Path p) throws IOException {
        String v = versionOfFile(p);
        if (v != null) {
            vers.computeIfAbsent(v, k -> new ArrayList<Path>()).add(p);
        }
    }
    public static void main(String[] args) throws IOException {
        ShowClassVersions v = new ShowClassVersions();
        var files = Arrays.stream(args).map(Path::of).collect(Collectors.toList());
        for (var f : files) {
            v.scan(f);
        }
        for (var ver : v.vers.keySet())  {
            System.out.println("Version: "+ver);
            for (var p : v.vers.get(ver)) {
                System.out.println("   "+p);
            }
        };
    }
}