From 54f34750edb284f4a49fb86c3939491211eb98fe Mon Sep 17 00:00:00 2001 From: liyazzi Date: Fri, 20 Dec 2024 07:05:42 +0000 Subject: [PATCH] 8331467: FileSystems.getDefault fails with ClassNotFoundException if custom default provider is in run-time image Reviewed-by: alanb, jpai --- .../internal/jimage/ImageReaderFactory.java | 24 +++++++++++++++---- .../java/nio/file/spi/SetDefaultProvider.java | 4 +--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/java.base/share/classes/jdk/internal/jimage/ImageReaderFactory.java b/src/java.base/share/classes/jdk/internal/jimage/ImageReaderFactory.java index 24ac9bfc18aff..2ecec20d6f9ba 100644 --- a/src/java.base/share/classes/jdk/internal/jimage/ImageReaderFactory.java +++ b/src/java.base/share/classes/jdk/internal/jimage/ImageReaderFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,8 +27,9 @@ import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.concurrent.ConcurrentHashMap; import java.util.Map; import java.util.Objects; @@ -47,8 +48,23 @@ public class ImageReaderFactory { private ImageReaderFactory() {} private static final String JAVA_HOME = System.getProperty("java.home"); - private static final Path BOOT_MODULES_JIMAGE = - Paths.get(JAVA_HOME, "lib", "modules"); + private static final Path BOOT_MODULES_JIMAGE; + + static { + FileSystem fs; + if (ImageReaderFactory.class.getClassLoader() == null) { + try { + fs = (FileSystem) Class.forName("sun.nio.fs.DefaultFileSystemProvider") + .getMethod("theFileSystem") + .invoke(null); + } catch (Exception e) { + throw new ExceptionInInitializerError(e); + } + } else { + fs = FileSystems.getDefault(); + } + BOOT_MODULES_JIMAGE = fs.getPath(JAVA_HOME, "lib", "modules"); + } private static final Map readers = new ConcurrentHashMap<>(); diff --git a/test/jdk/java/nio/file/spi/SetDefaultProvider.java b/test/jdk/java/nio/file/spi/SetDefaultProvider.java index a8581dc0e7ae2..ea154b5952edb 100644 --- a/test/jdk/java/nio/file/spi/SetDefaultProvider.java +++ b/test/jdk/java/nio/file/spi/SetDefaultProvider.java @@ -23,7 +23,7 @@ /* * @test - * @bug 4313887 7006126 8142968 8178380 8183320 8210112 8266345 8263940 + * @bug 4313887 7006126 8142968 8178380 8183320 8210112 8266345 8263940 8331467 * @modules jdk.jartool jdk.jlink * @library /test/lib * @build testfsp/* testapp/* @@ -45,7 +45,6 @@ import jdk.test.lib.process.ProcessTools; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.BeforeAll; import static org.junit.jupiter.api.Assertions.*; @@ -124,7 +123,6 @@ void testFspOnModulePath2() throws Exception { /** * Test file system provider linked into run-time image. */ - @Disabled @Test void testFspInRuntimeImage() throws Exception { String image = "image";