Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

check a file inside a WAR  RSS feed

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I want to check if a file exists in a deployed WAR (not expanded), this file can be located using Servlet Context .getRealPath("/image/myImage..").   My question ---
If I use



I assume that's the way to check. But the tricky part is the WAR isn't expanded, how does it figure out the file path when WAR isn't expanded ?  IS this a wrong way to do it ?
 
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDK package java.util.jar description: Provides classes for reading and writing the JAR (Java Archive) file format...
 
Linwood Hayes
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prasad.  But this is not .jar, it is a .war.
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A Java EE application is delivered in a Java Archive (JAR) file, a Web Archive (WAR) file, or an Enterprise Archive (EAR) file. A WAR or EAR file is a standard JAR (.jar) file with a .war or .ear extension." - from the topic Packaging Applications in Java EE 6 Tutorials: https://docs.oracle.com/javaee/6/tutorial/doc/bnaby.html
 
Linwood Hayes
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, I still don't get it.  I know  jar is an utility to handle a .jar .war file.  But my case is executing a java class inside a .war and this class needs to use the code snippet to determine the existence of some files that is also inside the .war.   
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Linwood Hayes
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?



I don't know what you are talking about.  In the beginning of my thread I gave the code. 
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Linwood Hayes
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



I know this thing. That's why I had my code snippet using the similar method.  My point is --- Usually "File file = new File(....);   file.exist();"  works for a file on a file system.  In this case, when there is only one .war file deployed to server, does the methods work fine without any problem.  I know the answer is 99.9% yes, but I just want to know how it still works.  If it doesn't work, why.
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is a link to servlet specification 3.0 document. The chapter 4.6 Resources has details of accessing resources that are part of the web application; and probably something useful to clarify your query.

http://download.oracle.com/otn-pub/jcp/servlet-3.0-fr-oth-JSpec/servlet-3_0-final-spec.pdf?AuthParam=1528260127_e383bca098b75cc1af65a34daf9c9be8
 
Bartender
Posts: 19656
92
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 getResource() works for an expanded WAR scenario as well ?
 
Linwood Hayes
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ?

 
Saloon Keeper
Posts: 9118
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:evil


Huh?
 
Tim Holloway
Bartender
Posts: 19656
92
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Linwood Hayes wrote:
Got it Tim.  Can I also assume getResourceAsStream() works for an expanded WAR scenario as well ?



Yes. Both methods are agnostic as far as whether the WAR has been exploded or not.

And really, Stephan, don't you use getResourceAsStream as part of your plans for world domination? 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!