JDK 9 unsafe import sun.misc.Launcher
Asked Answered
B

2

6

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.

Blakeslee answered 5/12, 2017 at 4:6 Comment(3)
could you add to the details what are you ultimately trying to achieve using the above line of code?Sirius
heya, JDK 9 have several 'internal' api restricted from external access.. you will need to find a replacement for it.Garda
sun.misc.Launcher was a JDK internal class in JDK 8 and older to create the built-in class loaders and so other initialization prior to calling your main method. The class does not exist in JDK 9.Contort
S
3

Class.getResource method can be invoked on any Class

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

UPDATE

Edited based on Feedback from members

Stormy answered 5/12, 2017 at 4:11 Comment(3)
ClassLoader.class.getResource("/") returns null unfortunately. Though MyClass.class.getResource("/") works perfectly.Blakeslee
ClassLoader returns null, so does Object, so I guess the class has to come from my src folder. Sorry if the following doesn't answer the question since I'm not an expert on this, but MyClass is the class in which the above code resides. I also tried replacing MyClass by other classes in my src folder and it works too.Blakeslee
And that has been working since Java 1.1; there never was a reason to use the private, implementation-specific sun. … class.Romilda
S
3

Calling the Class.getResource from any of the class in the module whose classes are being tried to access should work fine.

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

The reason why the answer by shazin might return null is probably that with ClassLoader being a caller for the getResource call:

returns null when the resource is a non-".class" resource in a package that is not open to the caller's module.

Since ClassLoader belongs to the package java.lang in the module java.base to which your module might not be open.

Also, do note the resolution of the getResource is split further for named and unnamed modules.

Sirius answered 5/12, 2017 at 6:4 Comment(1)
nullpointer is correct. If the resource is something that is included with your library or application when Class.getResource is the API to locate it. If you are looking to scan the class path for a resource that some other library or application includes then you use ClassLoader.getResource.Contort

© 2022 - 2024 — McMap. All rights reserved.