Skip to content

Commit

Permalink
JBR-8123 NPE because FileSystems.getDefault() is null with -Djava.uti…
Browse files Browse the repository at this point in the history
…l.zip.use.nio.for.zip.file.access=true
  • Loading branch information
mkartashev authored and vprovodin committed Jan 14, 2025
1 parent 2bb061c commit c08c2a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.security.AccessController;
import java.security.CodeSource;
import java.security.PrivilegedAction;
import java.nio.file.FileSystems;
import java.security.ProtectionDomain;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -2335,6 +2336,16 @@ private static void initPhase3() {
// set TCCL
Thread.currentThread().setContextClassLoader(scl);

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

// system is fully initialized
VM.initLevel(4);
}
Expand Down
17 changes: 17 additions & 0 deletions test/jdk/java/nio/file/spi/SetDefaultProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ private String ofClasspath(String... paths) {
return String.join(File.pathSeparator, paths);
}

/**
* Test the file system provider located in a jar with
* -Djava.util.zip.use.nio.for.zip.file.access=true
* that makes ZipFile use NIO to read the jar.
*/
public void testClassPathWithFileSystemProviderJarAndNioForZipFile() throws Exception {
String testClasses = System.getProperty("test.classes");
Path fspJar = Path.of("testFileSystemProvider.jar");
Files.deleteIfExists(fspJar);
createFileSystemProviderJar(fspJar, Path.of(testClasses));
String jarFile = createModularJar();
String classpath = ofClasspath(fspJar.toString(), jarFile, testClasses);
int exitValue = exec(SET_DEFAULT_FSP, "-Djava.util.zip.use.nio.for.zip.file.access=true",
"-cp", classpath, "p.Main");
assertEquals(exitValue, 0);
}

/**
* Returns the directory containing the classes for the given module.
*/
Expand Down

0 comments on commit c08c2a3

Please sign in to comment.