Skip to content

Commit

Permalink
Log JDK boot modules on startup.
Browse files Browse the repository at this point in the history
could be useful while inspecting user logs since it would identify
headless JDKs or JREs with non-standard module lists.
  • Loading branch information
mbien committed Dec 15, 2024
1 parent 1059e0b commit 75c335f
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Handler;
import java.util.logging.Level;
Expand All @@ -51,6 +53,7 @@
import java.util.logging.StreamHandler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.netbeans.TopSecurityManager;
import org.netbeans.core.startup.logging.NbLogging;
import org.openide.filesystems.FileUtil;
Expand Down Expand Up @@ -294,6 +297,7 @@ private void printSystemInfo(PrintStream ps) {
}
ps.println(" Application Classpath = " + cp); // NOI18N
ps.println(" Startup Classpath = " + System.getProperty("netbeans.dynamic.classpath", "unknown")); // NOI18N
ps.println(" Java Boot Modules = " + createJavaBootModuleList());
ps.println("-------------------------------------------------------------------------------"); // NOI18N
}

Expand All @@ -309,6 +313,32 @@ private static String createBootClassPath() {
return sb.toString();
}

private List<String> createJavaBootModuleList() {
// TODO JDK 11 equivalent
// return ModuleLayer.boot().modules().stream()
// .map(Module::getName)
// .sorted()
// .collect(Collectors.toList());
try {
Class<?> mod_class = Class.forName("java.lang.Module");
Class<?> ml_class = Class.forName("java.lang.ModuleLayer");
@SuppressWarnings("unchecked")
Set<?> mods = (Set<?>)ml_class.getDeclaredMethod("modules").invoke(
ml_class.getDeclaredMethod("boot").invoke(null)
);
return mods.stream().map(mod -> {
try {
return (String) mod_class.getMethod("getName").invoke(mod);
} catch (ReflectiveOperationException ex) {
return "unknown"; // outer try would fail first
}
})
.sorted().collect(Collectors.toList());
} catch (ReflectiveOperationException ex) {
return Collections.emptyList();
}
}

/** Scans path list for something that can be added to classpath.
* @param extensions null or path list
* @param sb buffer to put results to
Expand Down

0 comments on commit 75c335f

Please sign in to comment.