package com.ankurm.runtimeinfo;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.stream.Collectors;

/** Prints the Java runtime this program is running on: version, vendor, modules present, and size on disk. */
public class RuntimeInfo {
    public static void main(String[] args) throws IOException {
        System.out.println("java.version = " + System.getProperty("java.version"));
        System.out.println("java.vendor  = " + System.getProperty("java.vendor"));
        System.out.println("modules      = " + ModuleLayer.boot().modules().stream()
                .map(Module::getName).sorted().collect(Collectors.joining(", ")));
        Path home = Path.of(System.getProperty("java.home"));
        long[] total = {0};
        Files.walkFileTree(home, new SimpleFileVisitor<>() {
            @Override public FileVisitResult visitFile(Path f, BasicFileAttributes a) {
                total[0] += a.size();
                return FileVisitResult.CONTINUE;
            }
        });
        System.out.printf("runtime size = %.1f MB (%s)%n", total[0] / 1048576.0, home.getFileName());
    }
}
