JARパッケージを深く理解する


      Java       ,JAR      “  ” WAR   EAR          Ant   Maven        。         JAR       (  ,         )      ,     。
   ,JAR            ,      JAR      ,      。      5       ,               Java Archive   (       WAR   EAR),       。
      Java        Spring(   Spring        JAR            ),             Spring         JAR 。

        Java Archive              ,           。
     JAR  
  ,         ,        JAR   ,   jar        ,  ,      Ant jar     Java   (      )           ,      ,           ,            JAR。  ,       Hello,             ,                    。   1   :

  1.          
				
package com.tedneward.jars;

public class Hello
{
    public static void main(String[] args)
    {
        System.out.println("Howdy!");
    }
}

Hello          ,       JAR            “   ”,           。
   
1. JAR      
.NET   C++         OS    ,       (helloWorld.exe)     ,   GUI shell                 。    Java    ,      — java —   JVM       ,             (com.tedneward.Hello)        main()     。
          Java                。                  (        ),                           。
        JAR    “   ” ,   Java         JAR    ,           。       ,        JAR      (MANIFEST.MF   JAR   META-INF     ),   :

  2.      !
				
Main-Class: com.tedneward.jars.Hello

            。                 ,      JAR  ,   Ant            。   3  ,   Ant jar     manifest         :

  3.        !
				
    <target name="jar" depends="build">
        <jar destfile="outapp.jar" basedir="classes">
            <manifest>
                <attribute name="Main-Class" value="com.tedneward.jars.Hello" />
            </manifest>
        </jar>
    </target>

        JAR             java -jar outapp.jar            。  GUI shell   ,   JAR     。
   
2. JAR           
   Hello         ,           。Spring   Guice        (DI)             ,         :         DI            4      , :

  4. Hello、Spring world!
				
package com.tedneward.jars;

import org.springframework.context.*;
import org.springframework.context.support.*;

public class Hello
{
    public static void main(String[] args)
    {
        ApplicationContext appContext =
            new FileSystemXmlApplicationContext("./app.xml");
        ISpeak speaker = (ISpeak) appContext.getBean("speaker");
        System.out.println(speaker.sayHello());
    }
}

   Spring      
                 Spring   。
        -jar       -classpath            ,         ,Spring      CLASSPATH        。    ,JAR              JAR      ,               CLASSPATH,   5   :

  5. Hello、Spring CLASSPATH!
				
 <target name="jar" depends="build">
        <jar destfile="outapp.jar" basedir="classes">
            <manifest>
                <attribute name="Main-Class" value="com.tedneward.jars.Hello" />
                <attribute name="Class-Path" 
                    value="./lib/org.springframework.context-3.0.1.RELEASE-A.jar 
                      ./lib/org.springframework.core-3.0.1.RELEASE-A.jar 
                      ./lib/org.springframework.asm-3.0.1.RELEASE-A.jar 
                      ./lib/org.springframework.beans-3.0.1.RELEASE-A.jar 
                      ./lib/org.springframework.expression-3.0.1.RELEASE-A.jar 
                      ./lib/commons-logging-1.0.4.jar" />
            </manifest>
        </jar>
    </target>

   Class-Path                 JAR        。                     。     ,     JAR         JAR        。
    ,value     Ant Class-Path           ,   JAR           Class-Path   。  ,                   。  ,    ,     java -jar outapp.jar   ,     !
   
3. JAR        
               (        )    Spring   ,       Spring JAR         ,           。                JAR   。Java     JAR      ,      “    ” ,     lib/ext    ,  JRE        。
JRE          ,         Java        ,          lib/ext     JAR        ,           Java     CLASSPATH  。
   
4. Java 6         
        CLASSPATH     (Java              ) /     -classpath   ,Java 6              。                  JAR   ,        lib/*,    JAR         (   ),     。
    ,                Class-Path      。            Java     (     )      ,   code-gen        。
   
5. JAR        
Spring,     Java       ,                ,     ,Spring       app.xml   ,     JAR          —           JAR              ,     !
         sysadmin     ,         (   Hibernate   )    sysadmin    ,        。                        —      ,   JAR            “   ” ZIP   。       JAR  ,     Ant     jar              。
JAR             ,        。  ,     SpeakEnglish            ,         ,   6   :

  6.     
				
package com.tedneward.jars;

import java.util.*;

public class SpeakEnglish
    implements ISpeak
{
    Properties responses = new Properties();
    Random random = new Random();

    public String sayHello()
    {
        // Pick a response at random
        int which = random.nextInt(5);
        
        return responses.getProperty("response." + which);
    }
}

    responses.properties    JAR   ,       JAR               。      JAR       responses.properties     。
    JAR        ,            。          JAR        ,             ,        JAR      ,   JarFile         。  ,       ClassLoader    ,   JAR       “  ”   ,   ClassLoader getResourceAsStream()   ,   7   :

  7. ClassLoader     
				
package com.tedneward.jars;

import java.util.*;

public class SpeakEnglish
    implements ISpeak
{
    Properties responses = new Properties();
    // ...

    public SpeakEnglish()
    {
        try
        {
            ClassLoader myCL = SpeakEnglish.class.getClassLoader();
            responses.load(
                myCL.getResourceAsStream(
                    "com/tedneward/jars/responses.properties"));
        }
        catch (Exception x)
        {
            x.printStackTrace();
        }
    }
    
    // ...
}

                  :    、    、    ,  。               JAR  ,     InputStream   (   ClassLoader),           。

  ,    JAR        WAR     ,    (    Class-Path   Main-Class   )   WAR         ,   servlet         ,              ,  ,                 “  ,         ......”    ,         Java         。