Get class entries for running a web application

I need to read CLASSPATH entries for the current web application. In CLASSPATH , I have many files with the same name. I would like to check at what position they are displayed in the classpath. For example: path:\file.txt;path2:\file.txt...

Thank you for your help.

Regards Sebastian

+4
source share
4 answers

Try the following:

 // 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 separator and fileSep because : and \ highly system dependent.

+6
source

Using the sebastian answer above, I made this piece of code that helped.

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

He gave the following 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 
+4
source

In general, your files should be in WEB-INF / lib and WEB-INF / classes - this. What is the secret?

+2
source

based on Rok's answer, if you use Eclipse, run this code on the View tab when you are at the breakpoint and you will get the class path in the 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()); 
0
source

Source: https://habr.com/ru/post/1343308/


All Articles