Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 691
Jonathan Grimsdale Avatar asked Mar 27 '13 15:03

Jonathan Grimsdale


2 Answers

Use:

String pngpath = getClass().getResource("/resources/image.png").getFile();
File file = new File(pngpath);
System.out.println(file.getAbsolutePath());
like image 69
DiogoSantana Avatar answered Sep 19 '22 02:09

DiogoSantana


A constructor of File(uri) or File(string) helps to get file object from system dependent path string or URI object.

It is a solution to using the Java Library.

System.out.println(new File("/C:/Users/jgrimsdale").toString())

https://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.net.URI)

like image 23
Changwon Choe Avatar answered Sep 18 '22 02:09

Changwon Choe