• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

check a file inside a WAR

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prasad.  But this is not .jar, it is a .war.
 
Prasad Saya
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:evil


Huh?
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?  
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic