Google

Jun 16, 2014

Understanding JDK and JRE file structure. What is a jar file? What are war and ear files?

JDK1.6.0 → The root directory of the JDK software containing license, copyright, and readme files. Also contains src.zip, the archive of source code for the Java platform.

/JDK1.6.0/bin → The executable files like javac for all the development tools. The PATH environment variable should contain an entry to this directory.

/JDK1.6.0/lib → Files used by the development tools. Includes tools.jar, which contains non-core classes for support of the tools and utilities in the JDK.

/JDK1.6.0/jre → The root directory of the Java run-time environment. This the directory referred by the “java.home” system property.



/JDK1.6.0/jre/lib → Code libraries, property settings, and resource files used by the Java runtime environment. Includes:  
  • rt.jar – the Java platforms core API (the bootstrap classes). The rt most likely stands for RunTime. Some tend to think it stands for RooT, since this jar contains the root classes. 
  • charsets.jar – character conversion classes.

/JDK1.6.0/jre/lib/ext → This directory is for extensions to the Java platform. The localedata.jar contains locale data for java.text and java.util packages.

/JDK1.6.0/jre/lib/security → Contains security policy (java.policy) and security properties (java.security) files.


Q. What does jar stand for? How does it differ from a zip file?
A. The jar stands for Java ARchive. A jar file usually has a file name extension .jar. It mainly contains Java class files but any types of files can be included. For example, XML files, properties files, HTML files, image files, binary files, etc. You can use the “jar” application utility bundled inside /JDK1.6.0/jre/bin to create, extract, and view its contents. You can also use any zip file utility program to view its contents. A jar file cannot contain other jar files.

Basically, a jar file is same as a zip file, except that it contains a META-INF directory to store meta data or attributes. The most known file is META-INF/MANIFEST.MF. When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive. Most uses of JAR files beyond simple archiving and compression require special information to be in the manifest file. For example,

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application's entry point. The entry point is the class having a method with signature public static void main(String[ ] args). For example,

Main-Class: Test.class

A package within a JAR file can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file. You might want to seal a package, for example, to ensure version consistency among the classes in your software or as a security measure.

Name: myCompany/myPackage/ Sealed: true



Note: Refer JDK documentation for the manifest format and other options.

Core Java apps can be packaged as a jar file and run as

$java -cp MyApp.jar MyApp 

Where MyApp.java is the source file with the main method. The enterprise web applications need to be packaged as WAR or EAR files and deployed to a web container like Tomcat or an application server like JBoss.

Q. What are war and ear files, and how do they differ from jar files?
A. WAR (Web ARchive) and EAR (Enterprise ARchive) are like jar files, but have a certain structure, and unlike a jar file which can't have other jar files, a war and ear file can contain other jar files. An ear file can have other jar and war files.


Component type
Components
Packaged as
Applet
applets
JAR (Java ARchive)

Application client
Client side Java codes.
JAR (Java ARchive)

Web component
JSP, Servlet
WAR (Web ARchive)

Enterprise JavaBeans
Session beans, Entity beans, Message driven beans
JAR (EJB Archive)

Enterprise application
WAR, JAR, etc
EAR (Enterprise ARchive)

Resource adapters
Resource adapters
RAR (Resource Adapter ARchive)


Q. Do you have different class loaders to load these archives?
A. Class loaders are hierarchical as shown below. The structure may differ among the server vendors like JBoss, Weblogic, Websphere, Tomcat, etc.



The WAR and EAR files are deployable as an application and generally copied to the application server (e.g. JBoss) folder like "deploy" and the the server is started. Applications (e.g. war or ear) can also be deployed after starting the server, and then accessing its admin console to upload an ear or war file.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home