classloader获取.war中文件列表的路径

时间:2012-02-15 19:02:13

标签: java war ear

我不太确定我的类加载器路径应该是什么来获取战争中的文件列表。 这是我的结构:

.ear
|_ meta-inf
   |_ application.xml
|_ web.war
|_ lib
   |_ app.jar

MyApp.java位于app.jar中,MyApp.java想要列出web.war中的所有文件。我不确定是否可能。如果是这样,路径应该是什么?如果不可能,为什么不呢?

由于 -A

1 个答案:

答案 0 :(得分:0)

根据应用程序的部署方式,war文件可能不存在。例如,在解压缩后它可能会被删除。

如果战争已删除,如果您了解有关其MyApp.java的详细信息,则可以在文件系统上获取ClassLoader的位置。

由此可以假设 web.war 文件将位于相对于此的位置。

这可能会让你开始:

package stackoverflow;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Class clazz = getClass();
            ClassLoader cl = clazz.getClassLoader();
            System.out.println("ClassLoader=" + cl);

            // Where does the servlet itself live?
            String resourceNameForClass = clazz.getName().replaceAll("\\.", "/") + ".class";
            System.out.println("resourceNameForClass=" + resourceNameForClass);
            for (Enumeration<URL> e = cl.getResources(resourceNameForClass); e.hasMoreElements();) {
                System.out.println("resource=" + e.nextElement());
            }

            // What about a class that lives in a jar in WEB-INF/lib
            clazz = Class.forName("org.slf4j.Logger");
            resourceNameForClass = clazz.getName().replaceAll("\\.", "/") + ".class";
            System.out.println("resourceNameForClass=" + resourceNameForClass);
            for (Enumeration<URL> e = cl.getResources(resourceNameForClass); e.hasMoreElements();) {
                System.out.println("resource=" + e.nextElement());
            }

            if (cl instanceof URLClassLoader) {
                URLClassLoader ucl = (URLClassLoader) cl;
                URL[] urls = ucl.getURLs();
                for (int i = 0; i < urls.length; i++) {
                    System.out.println("url[" + i + "]=" + urls[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

产地:

ClassLoader         =com.google.appengine.tools.development.IsolatedAppClassLoader@1909385
resourceNameForClass=stackoverflow/TestServlet.class
resource            =file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/classes/stackoverflow/TestServlet.class
resourceNameForClass=org/slf4j/Logger.class
resource            =jar:file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/slf4j-api-1.6.4.jar!/org/slf4j/Logger.class
url[0]=file:/D:/apps/eclipse-jee-helios-win32/plugins/com.google.appengine.eclipse.sdkbundle_1.6.1.v201112160242r36/appengine-java-sdk-1.6.1/lib/impl/agent/appengine-agentruntime.jar
url[1]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/classes/
url[2]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/appengine-api-1.0-sdk-1.6.1.jar
url[3]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/appengine-api-labs-1.6.1.jar
url[4]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/appengine-jsr107cache-1.6.1.jar
url[5]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/datanucleus-appengine-1.0.10.final.jar
url[6]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/datanucleus-core-1.1.5.jar
url[7]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/datanucleus-jpa-1.1.5.jar
url[8]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/geronimo-jpa_3.0_spec-1.1.1.jar
url[9]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/geronimo-jta_1.1_spec-1.1.1.jar
url[10]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/google_sql.jar
url[11]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/gson-2.1.jar
url[12]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/jdo2-api-2.3-eb.jar
url[13]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/jsr107cache-1.1.jar
url[14]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/log4j-1.2.16.jar
url[15]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/slf4j-api-1.6.4.jar
url[16]=file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/slf4j-log4j12-1.6.4.jar
url[17]=file:/D:/apps/eclipse-jee-helios-win32/plugins/com.google.appengine.eclipse.sdkbundle_1.6.1.v201112160242r36/appengine-java-sdk-1.6.1/lib/tools/jsp/repackaged-appengine-jakarta-jstl-1.1.2.jar
url[18]=file:/D:/apps/eclipse-jee-helios-win32/plugins/com.google.appengine.eclipse.sdkbundle_1.6.1.v201112160242r36/appengine-java-sdk-1.6.1/lib/tools/jsp/repackaged-appengine-jakarta-standard-1.1.2.jar
url[19]=file:/D:/apps/eclipse-jee-helios-win32/plugins/com.google.appengine.eclipse.sdkbundle_1.6.1.v201112160242r36/appengine-java-sdk-1.6.1/lib/tools/jsp/repackaged-appengine-jasper-jdt-6.0.29.jar

在我的示例中,我知道的一个类位于 WEB-INF / lib (在我的情况下为org.slf4j.Logger)的jar中,它提供了以下资源URL:

resource=jar:file:/D:/dev/workspaces/3.6/all/grimbo9/war/WEB-INF/lib/slf4j-api-1.6.4.jar!/org/slf4j/Logger.class

从这里我可以解决战争被解压的地方。

你可以做类似的事情。当然,您的里程可能因应用服务器而异。