7878import org .graalvm .nativeimage .libgraal .hosted .LibGraalLoader ;
7979
8080import com .oracle .svm .core .NativeImageClassLoaderOptions ;
81+ import com .oracle .svm .core .SharedConstants ;
8182import com .oracle .svm .core .SubstrateOptions ;
8283import com .oracle .svm .core .SubstrateUtil ;
8384import com .oracle .svm .core .option .AccumulatingLocatableMultiOptionValue ;
@@ -114,6 +115,8 @@ public final class NativeImageClassLoaderSupport {
114115 private final List <Path > imagemp ;
115116 private final List <Path > buildmp ;
116117
118+ private final Set <Path > imageProvidedJars ;
119+
117120 private final EconomicMap <URI , EconomicSet <String >> classes ;
118121 private final EconomicMap <URI , EconomicSet <String >> packages ;
119122 private final EconomicSet <String > emptySet ;
@@ -245,6 +248,8 @@ protected NativeImageClassLoaderSupport(ClassLoader defaultSystemClassLoader, St
245248
246249 upgradeAndSystemModuleFinder = createUpgradeAndSystemModuleFinder ();
247250
251+ imageProvidedJars = parseImageProvidedJarsProperty ();
252+
248253 ModuleFinder modulePathsFinder = getModulePathsFinder ();
249254 Set <String > moduleNames = modulePathsFinder .findAll ().stream ()
250255 .map (moduleReference -> moduleReference .descriptor ().name ())
@@ -305,7 +310,7 @@ public List<ClassLoader> getClassLoaders() {
305310 return classLoaders ;
306311 }
307312
308- private ModuleFinder getModulePathsFinder () {
313+ public ModuleFinder getModulePathsFinder () {
309314 return ModuleFinder .of (imagemp .toArray (Path []::new ));
310315 }
311316
@@ -324,7 +329,12 @@ public void loadAllClasses(ForkJoinPool executor, ImageClassLoader imageClassLoa
324329 LogUtils .warning (msg );
325330
326331 var origin = new IncludeOptionsSupport .ExtendedOptionWithOrigin (new IncludeOptionsSupport .ExtendedOption ("" , PreserveOptionsSupport .PRESERVE_ALL ), preserveAllOrigin );
327- getModulePathsFinder ().findAll ().forEach (m -> preserveSelectors .addModule (m .descriptor ().name (), origin ));
332+ for (ModuleReference m : getModulePathsFinder ().findAll ()) {
333+ var modulePath = m .location ().map (Path ::of ).orElse (null );
334+ if (!imageProvidedJars .contains (modulePath )) {
335+ preserveSelectors .addModule (m .descriptor ().name (), origin );
336+ }
337+ }
328338 PreserveOptionsSupport .JDK_MODULES_TO_PRESERVE .forEach (moduleName -> preserveSelectors .addModule (moduleName , origin ));
329339 preserveSelectors .addModule (ALL_UNNAMED , origin );
330340 }
@@ -775,6 +785,18 @@ static Optional<String> getMainClassFromModule(Object module) {
775785 return ((Module ) module ).getDescriptor ().mainClass ();
776786 }
777787
788+ private static Set <Path > parseImageProvidedJarsProperty () {
789+ Set <Path > imageProvidedJars = new HashSet <>();
790+ String args = System .getProperty (SharedConstants .IMAGE_PROVIDED_JARS_ENV_VARIABLE , "" );
791+ if (!args .isEmpty ()) {
792+ String [] parts = args .split (File .pathSeparator );
793+ for (String part : parts ) {
794+ imageProvidedJars .add (Path .of (part ));
795+ }
796+ }
797+ return Collections .unmodifiableSet (imageProvidedJars );
798+ }
799+
778800 private final class LoadClassHandler {
779801
780802 private final ForkJoinPool executor ;
@@ -1180,7 +1202,12 @@ public void setPreserveAll(ValueWithOrigin<String> valueWithOrigin) {
11801202
11811203 public void setTrackAllDynamicAccess (ValueWithOrigin <String > valueWithOrigin ) {
11821204 var origin = new IncludeOptionsSupport .ExtendedOptionWithOrigin (new IncludeOptionsSupport .ExtendedOption ("" , DynamicAccessDetectionFeature .TRACK_ALL ), valueWithOrigin );
1183- getModulePathsFinder ().findAll ().forEach (m -> dynamicAccessSelectors .addModule (m .descriptor ().name (), origin ));
1205+ for (ModuleReference m : getModulePathsFinder ().findAll ()) {
1206+ var modulePath = m .location ().map (Path ::of ).orElse (null );
1207+ if (!imageProvidedJars .contains (modulePath )) {
1208+ dynamicAccessSelectors .addModule (m .descriptor ().name (), origin );
1209+ }
1210+ }
11841211 dynamicAccessSelectors .addModule (ALL_UNNAMED , origin );
11851212 }
11861213
@@ -1307,7 +1334,9 @@ public void addModule(String moduleName, IncludeOptionsSupport.ExtendedOptionWit
13071334 IncludeOptionsSupport .ExtendedOptionWithOrigin includeOptionsSupport = new IncludeOptionsSupport .ExtendedOptionWithOrigin (extendedOptionWithOrigin .option (),
13081335 extendedOptionWithOrigin .valueWithOrigin ());
13091336 for (Path path : applicationClassPath ()) {
1310- classpathEntries .put (path , includeOptionsSupport );
1337+ if (!imageProvidedJars .contains (path )) {
1338+ classpathEntries .put (path , includeOptionsSupport );
1339+ }
13111340 }
13121341 } else {
13131342 moduleNames .put (moduleName , extendedOptionWithOrigin );
0 commit comments