Skip to content

Commit c08c2a3

Browse files
mkartashevvprovodin
authored andcommitted
JBR-8123 NPE because FileSystems.getDefault() is null with -Djava.util.zip.use.nio.for.zip.file.access=true
1 parent 2bb061c commit c08c2a3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/java.base/share/classes/java/lang/System.java

+11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.security.AccessController;
5454
import java.security.CodeSource;
5555
import java.security.PrivilegedAction;
56+
import java.nio.file.FileSystems;
5657
import java.security.ProtectionDomain;
5758
import java.util.Collections;
5859
import java.util.List;
@@ -2335,6 +2336,16 @@ private static void initPhase3() {
23352336
// set TCCL
23362337
Thread.currentThread().setContextClassLoader(scl);
23372338

2339+
if (Boolean.getBoolean("java.util.zip.use.nio.for.zip.file.access")
2340+
&& System.getProperty("java.nio.file.spi.DefaultFileSystemProvider") != null) {
2341+
// Make sure the custom file system(s) are loaded using the "standard" ZipFile operating mode
2342+
// rather than the one that forwards to NIO. The latter will use the file system that is being loaded to
2343+
// try to load files, which will result in an NPE from FileSystems.getDefault().
2344+
// Calling FileSystems.getDefault() here bypasses that because the NIO operating mode of ZipFile only
2345+
// activates at init level 4.
2346+
FileSystems.getDefault();
2347+
}
2348+
23382349
// system is fully initialized
23392350
VM.initLevel(4);
23402351
}

test/jdk/java/nio/file/spi/SetDefaultProvider.java

+17
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,23 @@ private String ofClasspath(String... paths) {
201201
return String.join(File.pathSeparator, paths);
202202
}
203203

204+
/**
205+
* Test the file system provider located in a jar with
206+
* -Djava.util.zip.use.nio.for.zip.file.access=true
207+
* that makes ZipFile use NIO to read the jar.
208+
*/
209+
public void testClassPathWithFileSystemProviderJarAndNioForZipFile() throws Exception {
210+
String testClasses = System.getProperty("test.classes");
211+
Path fspJar = Path.of("testFileSystemProvider.jar");
212+
Files.deleteIfExists(fspJar);
213+
createFileSystemProviderJar(fspJar, Path.of(testClasses));
214+
String jarFile = createModularJar();
215+
String classpath = ofClasspath(fspJar.toString(), jarFile, testClasses);
216+
int exitValue = exec(SET_DEFAULT_FSP, "-Djava.util.zip.use.nio.for.zip.file.access=true",
217+
"-cp", classpath, "p.Main");
218+
assertEquals(exitValue, 0);
219+
}
220+
204221
/**
205222
* Returns the directory containing the classes for the given module.
206223
*/

0 commit comments

Comments
 (0)