Search code examples
javajava-9getresource

JDK 9 unsafe import sun.misc.Launcher


I've recently upgraded to JDK 9 and Eclipse complains that sun.misc.Launcher cannot be imported. It seems that sun.misc.Launcher is unsafe. I'm looking for an alternative to replace this line of code in my project.

final URL url = Launcher.class.getResource("/");

Any help would be appreciated.

Update: the more complete version of the above block of code is:

final URL url = Launcher.class.getResource("/");
final File fs = new File(url.toURI());
for (File f : fs.listFiles()) {
     System.out.println(f.getAbsolutePath());
}

This is to print all the files in the src folder when program is launched in IDE.


Solution

  • Class.getResource method can be invoked on any Class

    final URL url = ClassInTheCurrentModule.class.getResource("/");
    

    UPDATE

    Edited based on Feedback from members