Get the absolute path of a file on the class path
<h3>Setup</h3>

<ul>
<li>Pasted is modified version of an <a href="https://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java" rel="nofollow">Oracle example</a></li>
<li>While the program's running, if you now make a new file or directory directory inside of the <code>path</code> directory, you'll see that the path displayed is incorrect</li>
<li>For example, when I run it, it shows the absolute path of a newly-created file in the <code>path</code> directory as <code>/home/hoagy/test/new_file</code>, when the actual path to the file is <code>/home/hoagy/test/path/new_file</code></li>
</ul>

<h3>Question</h3>

<ul>
<li>How do I get the proper absolute path?</li>
</ul>

<h3>Code</h3>

<pre><code>package path.question;

import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.LinkOption.*;
import java.nio.file.attribute.*;
import java.io.*;
import java.util.*;

public class WatchDir {

    private final WatchService watcher;
    private final Map&lt;WatchKey,Path&gt; keys;

    @SuppressWarnings("unchecked")
    static &lt;T&gt; WatchEvent&lt;T&gt; cast(WatchEvent&lt;?&gt; event) {
        return (WatchEvent&lt;T&gt;)event;
    }

    private void register(Path dir) throws IOException {
        WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        keys.put(key, dir);
    }

    WatchDir(Path dir) throws IOException {
        this.watcher = FileSystems.getDefault().newWatchService();
        this.keys = new HashMap&lt;WatchKey,Path&gt;();
        register(dir);
    }

    void processEvents() {
        for (;;) {

            WatchKey key;
            try { key = watcher.take(); }
            catch (InterruptedException x) { return; }

            Path dir = keys.get(key);
            if (dir == null) {
                continue;
            }

            for (WatchEvent&lt;?&gt; event: key.pollEvents()) {
                WatchEvent.Kind kind = event.kind();

                if (kind == OVERFLOW) { continue; }

                WatchEvent&lt;Path&gt; ev = cast(event);
                Path name = ev.context();
                Path child = dir.resolve(name);

                System.out.println("Full path: " + name.toAbsolutePath().toString());
            }

            key.reset();
        }
    }

    public static void main(String[] args) throws IOException {
        Path dir = Paths.get("./path");
        new WatchDir(dir).processEvents();
    }
}
</code></pre>