getResource puts a leading / before the disk name using java 1.7 windows 7

Jonathan Grimsdale picture Jonathan Grimsdale · Mar 27, 2013 · Viewed 9.2k times · Source

The following gives a leading slash before the disk name. How can I avoid that?

String pngpath = getClass().getResource("/resources/image.png").getPath();
System.out.println("pngpath = "+pngpath);

Gives:

pngpath = /C:/Users/jgrimsdale/Documents/NetBeansProjects/HelloCV/build/classes/resources/image.png

Answer

DiogoSantana picture DiogoSantana · Mar 27, 2013

Use:

String pngpath = getClass().getResource("/resources/image.png").getFile();
File file = new File(pngpath);
System.out.println(file.getAbsolutePath());