解惑

解己之惑,解人之惑

标签:jar

AR何其多

看了新公司的发布目录,感叹公司把Java相关的发布包用得出神入化,从最简单的Jar到最常见的War,以及中大型J2EE工程中的Ear,然后是JBoss特定的Sar,以及被Jboss收购后的Hibernate提供的Har,真的是被AR搞混头了,特此列举一下:

  • JAR:java类文件的打包发布
  • WAR:WEB应用的打包发布
  • EAR:J2EE应用的打包发布,一般包括EJB
  • SAR:JBoss的Service打包发布
  • HAR:Hibernate打包发布

2007年7月13日更新:
一个E文的,好像比我的全面:

Intro to JARs, WARs, SARs, RARs and EARs

Anyone worth calling oneself a java developer would know what a JAR file is. It’s an archive, just like ZIPs and TARs are. In addition to being a simple archive, a JAR file can store some very useful meta information about its contents. A special file, META-INF/MANIFEST.MF is where a jar records meta information.

EJBs are packaged as jar files as well, albeit with one or more special files under META-INF, called deployment descriptors.

A WAR file packages a web application. What does a web application consist of? It consists of

  1. HTML/JSP pages and other resources such as images/javascript files/stylesheets that need to be reachable via URLs.
  2. Additional files such as java servlets, utility classes and jars for use in servlets/JSPs and deployment descriptors that should not be reachable via URLs.

The latter are stored under a special folder named WEB-INF. Both are then archived in JAR whose extension is modified as WAR to distinguish it as a java web application.

Similarly, RAR files are JAR files used to package resource adapter modules as defined under the J2EE Connector Architecture (JCA) and SAR files are the ones used to package JMX-enabled services in JBoss.

Just as all rivers are destined to go merge with the sea, all these archives that relate to a single enterprise application may be packaged under one EAR.

在目录中查找类位于哪个jar包中

做Java开发的人可能都遇到过面对一大堆的jar包但是自己不知道需要的那些个类到底位于哪个jar包中,使用下面的代码,这个问题就可以迎刃而解了。
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class FindInJar {
    public String className;

    public ArrayList jarFiles = new ArrayList();

    public FindInJar() {
    }

    public FindInJar(String className) {
        this.className = className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public List findClass(String dir, boolean recurse) {
        searchDir(dir, recurse);
        return this.jarFiles;
    }

    protected void searchDir(String dir, boolean recurse) {
        try {
            File d = new File(dir);
            if (!d.isDirectory()) {
                return;
            }
            File[] files = d.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (recurse && files[i].isDirectory()) {
                    searchDir(files[i].getAbsolutePath(), true);
                } else {
                    String filename = files[i].getAbsolutePath();
                    if (filename.endsWith(".jar")||filename.endsWith(".zip")) {
                        ZipFile zip = new ZipFile(filename);
                        Enumeration entries = zip.entries();
                        while (entries.hasMoreElements()) {
                            ZipEntry entry = (ZipEntry) entries.nextElement();
                            String thisClassName = getClassName(entry);
                            if (thisClassName.equals(this.className) || thisClassName.equals(this.className + ".class")) {
                                this.jarFiles.add(filename);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List getFilenames() {
        return this.jarFiles;
    }

    protected String getClassName(ZipEntry entry) {
        StringBuffer className = new StringBuffer(entry.getName().replace(‘/’, ‘.’));
        return className.toString();
    }

    public static void main(String args[]) {
        FindInJar findInJar = new FindInJar("javax.mail.Session");
        List jarFiles = findInJar.findClass("d:/libs/", true);
        if (jarFiles.size() == 0) {
            System.out.println("Not Found");
        } else {
            for (int i = 0; i < jarFiles.size(); i++) {
                System.out.println(jarFiles.get(i));
            }
        }
    }
}

main方法中的findClass方法的第二个参数是是否对指定的目录递归进行处理,一般都会要这样做的吧。

© 2024 解惑

本主题由Anders Noren提供向上 ↑