JDK 9 unsafe import sun.misc.Launcher

I recently upgraded to JDK 9, and Eclipse complains that it sun.misc.Launchercould not be imported. It seems sun.misc.Launcherunsafe. I am 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: a more complete version of the above code block:

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 for printing all the files in the folder srcwhen the program starts in the IDE.

+4
source share
2 answers

Class.getResource the method can be called on any Class

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

UPDATE

Edited based on feedback from participants.

+2
source

A call Class.getResourcefrom any class in the module whose classes tried to access should work fine.

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

, shazin null, , , ClassLoader, getResource:

null, . "" .

ClassLoader java.lang java.base, .

, getResource .

+4

Source: https://habr.com/ru/post/1690250/


All Articles