gmethvin / directory-watcher

A cross-platform Java recursive directory watcher, with a JNA macOS watcher and Scala better-files integration
Apache License 2.0
264 stars 34 forks source link

Fails with NoClassDefFoundError inside Wildfly #94

Open Artur- opened 1 year ago

Artur- commented 1 year ago

When running inside Wildfly 27.0.1.Final, a call to waitAsync() fails with

12:01:30,947 ERROR [stderr] (default task-2) Caused by: java.lang.NoClassDefFoundError: com/sun/nio/file/ExtendedWatchEventModifier
12:01:30,947 ERROR [stderr] (default task-2)    at deployment.project-base-flow-cdi-1.0-SNAPSHOT.war//io.methvin.watcher.DirectoryWatcher.register(DirectoryWatcher.java:454)
12:01:30,947 ERROR [stderr] (default task-2)    at deployment.project-base-flow-cdi-1.0-SNAPSHOT.war//io.methvin.watcher.DirectoryWatcher.registerAll(DirectoryWatcher.java:433)
12:01:30,947 ERROR [stderr] (default task-2)    at deployment.project-base-flow-cdi-1.0-SNAPSHOT.war//io.methvin.watcher.DirectoryWatcher.registerPaths(DirectoryWatcher.java:282)
12:01:30,947 ERROR [stderr] (default task-2)    at deployment.project-base-flow-cdi-1.0-SNAPSHOT.war//io.methvin.watcher.DirectoryWatcher.watchAsync(DirectoryWatcher.java:229)
12:01:30,947 ERROR [stderr] (default task-2)    at deployment.project-base-flow-cdi-1.0-SNAPSHOT.war//io.methvin.watcher.DirectoryWatcher.watchAsync(DirectoryWatcher.java:215)

The problematic code is

    WatchEvent.Modifier[] modifiers =
        useFileTreeModifier
            ? new WatchEvent.Modifier[] {ExtendedWatchEventModifier.FILE_TREE}
            : new WatchEvent.Modifier[] {};

and as it throws a NoClassDefFoundError, the case is not handled by

      } catch (UnsupportedOperationException e) {
        // UnsupportedOperationException should only happen if FILE_TREE is unsupported
        logger.debug("Assuming ExtendedWatchEventModifier.FILE_TREE is not supported", e);
        fileTreeSupported = false;
        // If we failed to use the FILE_TREE modifier, try again without
        registerAll(start, context);
      }
gmethvin commented 1 year ago

I guess if you still want it to work without having to have com.sun.nio.file on the classpath, then we could also catch the NoClassDefFoundError, but that modifier provides much better accuracy and performance when it is supported, so do we really want to do that? I'm not familiar with Wildfly in particular so I'm not sure about the impact there.

borkdude commented 2 months ago

This might be related. I'm getting this error on GraalVM 22:

Execution error (NoSuchMethodError) at com.oracle.truffle.api.library.LibraryFactory/ensureLibraryInitialized (LibraryFactory.java:384).
'void sun.misc.Unsafe.ensureClassInitialized(java.lang.Class)'