SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
Prasad Saya wrote:This is the issue: A Java class inside a WAR file (when run) needs to check existence of a file - which is also inside the *same* WAR file. How to do this?
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
Prasad Saya wrote:Look at this post: https://coderanch.com/t/356774/java/read-resource-file-war-file
Also try searching the internet using the search string for possible answers: java servlet read a file within the war
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Tim Holloway wrote:getRealPath returns null for a file resource in a WAR file, because there is no file path for a file in an unexploded WAR. Or, rather, the closest thing to a filesystem path is the path of the entire WAR file.
What you want isn't getRealPath, it's getResource() or its evil sibling getResourceAsStream(). The path for a resource is rooted at the root of the WAR, regardless of where in the filesystem the WAR is located, so your resource path is likewise "/image/myImage.."
Linwood Hayes wrote:
Tim Holloway wrote:getRealPath returns null for a file resource in a WAR file, because there is no file path for a file in an unexploded WAR. Or, rather, the closest thing to a filesystem path is the path of the entire WAR file.
What you want isn't getRealPath, it's getResource() or its evil sibling getResourceAsStream(). The path for a resource is rooted at the root of the WAR, regardless of where in the filesystem the WAR is located, so your resource path is likewise "/image/myImage.."
Got it Tim. Can I also assume getResourceAsStream() works for an expanded WAR scenario as well ?
Tim Holloway wrote:evil
Linwood Hayes wrote:
Got it Tim. Can I also assume getResourceAsStream() works for an expanded WAR scenario as well ?
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |