Get classpath entries for executing web application
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
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.
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
In general, your files should be in WEB-INF/lib and WEB-INF/classes - that's it. What's the mystery?
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());
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;
}
}
精彩评论