I'm new in java. I'm using eclipse to develop in java. I have a folder in project directory named "include", and in that folder, I've a file named "trial.jpg". I want to get the full path (with drive name, folder name and jar name after compiled). Please help me. Thank you.
Asked
Active
Viewed 72 times
-1
-
Why? `File f = new File("./include/trial.jpg"); f.getAbsolutePath();` will get you the full path – Dan Dec 30 '14 at 14:06
-
You are probably looking for [Class.getResource](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)) but it returns a URL because a `File` does not make sense in the case of a resource embedded in a Jar file. – Guillaume Polet Dec 30 '14 at 14:11
-
In output string, an extra dot (.) is added (e.g. D:\SamplePro\.\include\trial.jpg). – Girish Dec 30 '14 at 14:15
1 Answers
1
You can get the path to your jar by doing:
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
as suggested in this answer.
But, if you're trying to access the 'trial.jpg' from your code, you don't need a full path to it. It should already be in your classpath and you should be able to reference it directly.
Something along the lines of
InputStream is = this.getClass().getClassLoader().getResourceAsStream("include/trial.jpg");

Community
- 1
- 1

Martin Ivančič
- 121
- 1
- 3