"Java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.FileSystems $ DefaultFileSystemHolder" occurs even though I can check the classpath and encoding many times.

Even if I search on the net, there is no information, so I will write it for sharing purposes.

Cause

Occurs when a non-existent directory is set as the current directory.

I don't know if all the other filesystems have the same specifications, but at least in RedHat7, if you delete the current directory, you will be in a directory that does not exist.

[root@******** ~]$ cd hoge
[root@******** hoge]$ rmdir ../hoge
[root@******** hoge]$ 

Occurs when the initialization process of the DefaultFileSystemHolder class is executed in this state. Since it is used in the java.nio.file.Paths class, there seems to be a good chance that the initialization process will run.

What happened in my environment

A failure report was raised that the following exception occurred when a certain program was executed.

java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.FileSystems$DefaultFileSystemHolder
        at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
        at java.nio.file.Paths.get(Paths.java:84)
・ ・ ・

Since NoClassDefFoundError is displayed, check the classpath and jar, but it doesn't matter how many times you check. If you try google, you will find a JDK bug report that "occurs when file.encoding is cp037 "like here However, I haven't touched file.encoding in the first place ... Or rather, it does not reproduce even if it is executed in the same way on the same server as the failure report.

This program sends files in a certain directory to the outside, and deletes that directory when it finishes sending. When I carefully reviewed the screenshots in the failure report, it was executed with the directory to be deleted being the current directory. If you try the same thing, it will be reproduced.


After a little debugging, it seems that "sun.nio.fs.UnixException: There is no such file or directory" occurred during the initialization process of DefaultFileSystemHolder, but the root occurrence point has not been overtaken. Hmm. I hope that someone who is familiar with the information in this article will look it up.

Recommended Posts

"Java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.FileSystems $ DefaultFileSystemHolder" occurs even though I can check the classpath and encoding many times.
"Error: Main class XXX not found or could not be loaded" occurs even though the main class exists
The mysterious java.exe was the reason why javac and java versions could not be matched no matter how many times I tried.