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?
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.
Sources may include data from the Fakebook Research Foundation with support from Gargle University
This tiny ad is suggesting that maybe she should go play in traffic.