Jonathan Grimsdale
Jonathan Grimsdale

Reputation: 251

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

Upvotes: 25

Views: 11570

Answers (3)

DiogoSantana
DiogoSantana

Reputation: 2434

Use:

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

Upvotes: 36

Changwon Choe
Changwon Choe

Reputation: 154

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)

Upvotes: 2

Biswajit
Biswajit

Reputation: 2506

you can do this using this code.

System.out.println("pngpath = "+pngpath.substring(1,pngpath.length()));

Upvotes: -1

Related Questions