获取用于执行 Web 应用程序的类路径条目

发布于 2024-10-21 06:56:15 字数 163 浏览 3 评论 0原文

我需要读取当前 Web 应用程序的 CLASSPATH 条目。在 CLASSPATH 中,我有许多同名的文件。我想检查它们在类路径中出现的位置。例如:路径:\file.txt;path2:\file.txt...

感谢您的帮助。

亲切的问候 塞巴斯蒂安

i need to read CLASSPATH entries for current web application. In CLASSPATH i have many files with this same name. I would like to check on which position they appear in classpath. For example: path:\file.txt;path2:\file.txt....

Thank you for your help.

Kind regards
Sebastian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

指尖上得阳光 2024-10-28 06:56:16

一般来说,您的文件应该位于 WEB-INF/lib 和 WEB-INF/classes 中 - 就是这样。其中有何奥秘?

In general, your files should be in WEB-INF/lib and WEB-INF/classes - that's it. What's the mystery?

我三岁 2024-10-28 06:56:16

根据 Rok 的回答,如果您使用 Eclipse,当您处于断点时,在“查看”选项卡中运行此代码,您将获得文件中的类路径

StringBuilder sb = new StringBuilder();
java.net.URL[] urls=((java.net.URLClassLoader)getClass().getClassLoader()).getURLs();
for (java.net.URL u : urls) {
  sb.append(u+"\n");
}
org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File("/tmp/classpath"), sb.toString());

based on Rok answer, if you use Eclipse, run this code in "View" tab when you are in a breakpoint, and you will get the classpath in a file

StringBuilder sb = new StringBuilder();
java.net.URL[] urls=((java.net.URLClassLoader)getClass().getClassLoader()).getURLs();
for (java.net.URL u : urls) {
  sb.append(u+"\n");
}
org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File("/tmp/classpath"), sb.toString());
ゞ花落谁相伴 2024-10-28 06:56:16

我创建了这段代码(希望有帮助):

@Slf4j
public class ClasspathLibrariesParser {
    private static final List<String> ALLOWED_KINDS = Arrays.asList("lib", "con");
    private static final SAXParserFactory PARSER_FACTORY = ClasspathLibrariesParser.createParserFactory();
    
    private static SAXParserFactory createParserFactory() {
        final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        parserFactory.setNamespaceAware(true);
        return parserFactory;
    }

    public Set<String> parseClasspathFile() {
        final File file = new File("./.classpath");
        final Set<String> paths = this.parseClasspathFile(file);
        return paths;
    }

    public Set<String> parseClasspathFile(final File file) {
        try {
            final Set<String> paths = new TreeSet<>();
            final XMLReader parser = this.createClasspathFileParser(paths);
            parser.parse(file.toURI().toURL().toString());
            return paths;
        } catch (Exception e) {
            log.error("An error happened while parsing the classpath file \"{}\". Exception: {}", file, e.getMessage());
            return null;
        }
    }

    private XMLReader createClasspathFileParser(final Set<String> paths)
            throws SAXException, ParserConfigurationException {
        final XMLReader parser = PARSER_FACTORY.newSAXParser().getXMLReader();
        parser.setContentHandler(new DefaultHandler() {
            @Override
            public void startElement(final String uri, final String localName, final String qname,
                    final Attributes atts) {
                if (!"classpathentry".equals(localName) || atts == null) {
                    return;
                }

                for (int i = 0; i < atts.getLength(); i++) {
                    log.debug("read classpathentry attributes [{}] {} -> {}", i, atts.getLocalName(i), atts.getValue(i));
                }

                final String kind = atts.getValue("kind");
                if (kind != null && ALLOWED_KINDS.contains(kind)) {
                    final String path = atts.getValue("path");
                    paths.add(path);
                }
            }
        });

        return parser;
    }
}

I have created this code (hope it helps):

@Slf4j
public class ClasspathLibrariesParser {
    private static final List<String> ALLOWED_KINDS = Arrays.asList("lib", "con");
    private static final SAXParserFactory PARSER_FACTORY = ClasspathLibrariesParser.createParserFactory();
    
    private static SAXParserFactory createParserFactory() {
        final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        parserFactory.setNamespaceAware(true);
        return parserFactory;
    }

    public Set<String> parseClasspathFile() {
        final File file = new File("./.classpath");
        final Set<String> paths = this.parseClasspathFile(file);
        return paths;
    }

    public Set<String> parseClasspathFile(final File file) {
        try {
            final Set<String> paths = new TreeSet<>();
            final XMLReader parser = this.createClasspathFileParser(paths);
            parser.parse(file.toURI().toURL().toString());
            return paths;
        } catch (Exception e) {
            log.error("An error happened while parsing the classpath file \"{}\". Exception: {}", file, e.getMessage());
            return null;
        }
    }

    private XMLReader createClasspathFileParser(final Set<String> paths)
            throws SAXException, ParserConfigurationException {
        final XMLReader parser = PARSER_FACTORY.newSAXParser().getXMLReader();
        parser.setContentHandler(new DefaultHandler() {
            @Override
            public void startElement(final String uri, final String localName, final String qname,
                    final Attributes atts) {
                if (!"classpathentry".equals(localName) || atts == null) {
                    return;
                }

                for (int i = 0; i < atts.getLength(); i++) {
                    log.debug("read classpathentry attributes [{}] {} -> {}", i, atts.getLocalName(i), atts.getValue(i));
                }

                final String kind = atts.getValue("kind");
                if (kind != null && ALLOWED_KINDS.contains(kind)) {
                    final String path = atts.getValue("path");
                    paths.add(path);
                }
            }
        });

        return parser;
    }
}
任谁 2024-10-28 06:56:15

尝试这些:

// get the compact classpath value
String path = System.getProperty("java.class.path");

// the character : on windows and ; on unixes
String separator = System.getProperty("path.separator");

// the character \ on windows and / on unixes
String fileSep = System.getProperty("file.separator");

您需要 separatorfileSep 因为 :\ 高度依赖于系统。

Try these:

// get the compact classpath value
String path = System.getProperty("java.class.path");

// the character : on windows and ; on unixes
String separator = System.getProperty("path.separator");

// the character \ on windows and / on unixes
String fileSep = System.getProperty("file.separator");

You need the separator and fileSep because : and \ are highly system-dependant.

梨涡少年 2024-10-28 06:56:15

使用上面的塞巴斯蒂安答案,我编写了这段代码,这对我来说很有效。

ClassLoader c=getClass().getClassLoader();
logmsg("c="+c);
URLClassLoader u=(URLClassLoader)c;
URL[] urls=u.getURLs();
for (URL i : urls) {
    logmsg("url: "+i);
}

它给出了这个结果:

classpath=/dd/apache-tomcat-7.0.29/bin/bootstrap.jar:/dd/apache-tomcat-7.0.29/bin/tomcat-juli.jar
c=WebappClassLoader
  context: /xxx
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@35c0e45a

url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/classes/
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/commons-codec.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/commons-fileupload.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/commons-logging-api.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/commons-logging.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/freemarker.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/h2.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/js.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/log4j-1.2.8.jar

Using sebastian answer above, I made this piece of code, which did the trick for me.

ClassLoader c=getClass().getClassLoader();
logmsg("c="+c);
URLClassLoader u=(URLClassLoader)c;
URL[] urls=u.getURLs();
for (URL i : urls) {
    logmsg("url: "+i);
}

It gave this result:

classpath=/dd/apache-tomcat-7.0.29/bin/bootstrap.jar:/dd/apache-tomcat-7.0.29/bin/tomcat-juli.jar
c=WebappClassLoader
  context: /xxx
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@35c0e45a

url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/classes/
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/commons-codec.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/commons-fileupload.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/commons-logging-api.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/commons-logging.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/freemarker.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/h2.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/js.jar
url: file:/DT/DEV/dev/mko/apache-tomcat-7.0.29/webapps/xxx/WEB-INF/lib/log4j-1.2.8.jar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文