• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Is there a way to tell if a project is deployed as a jar or a war file?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a homegrown dependency injection/web framework project that I am working on. I have been developing against embedded tomcat projects but ran into an issue when deploying as a .war file... it has to do with the class path. I need to determine the class path based on whether the project is a .jar or .war, any ideas?

Mike
 
Saloon Keeper
Posts: 24214
167
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
Yes, in fact, there is.

A JAR file cannot be deployed as a webapp. Only WARs can be deployed.

Sort of. Actually, a JAR file is a ZIP file that contains Java metadata members. A WAR file is a JAR file that is organized as a WAR. Meaning that it has a WEB-INF directory which generally contains a web.xml file, a classes directory and/or a lib directory.

The classpath for a webapp consists of all the loose classes and resources under WEB-INF/classes plus all of the classes and resources that are in each of the JAR files in the WEB-INF/lib directory. With the disclaimer that just as in regular Java, the classpath does not include JARs embedded inside other JARs. The webapp's classpath further includes classes and resources that Tomcat itself provides - most notably those in the Tomcat lib directory.

Further notes: Tomcat runs several different subsystems, so in addition to the unique classpath that each webapp has, there are other classpaths for the different internal components of Tomcat. But you shouldn't ordinarily have to be concerned with them.

Also, Tomcat's default action when a WAR file is deployed into the TOMCAT_HOME/webapps directory is to immediately explode (unzip) it. Meaning that you'll have 2 copies of the webapp in the webapps directory, but only the exploded one is actually used. But on no account should you EVER attempt to locate class resources as files, because that's not guaranteed behavior and members of ZIP/JAR/WAR file are not accesible via standard filesystem I/O.

Likewise, you generally should not use the standard classpath methods to locate class resources in a webapp. Use the JEE ServletContext getResource/getResourceAsStream methods.
 
This tiny ad is suggesting that maybe she should go play in traffic.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic