java 检查jar文件是否加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9909334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if jar file is loaded?
提问by latestVersion
I have a custom jar file in the tomcat lib/ folder. As per my knowledge, any jar file in this folder will be loaded on startup.
我在 tomcat lib/ 文件夹中有一个自定义 jar 文件。据我所知,此文件夹中的任何 jar 文件都将在启动时加载。
Is there anyway I can check if a particular jar file is loaded or not?
无论如何我可以检查是否加载了特定的 jar 文件?
I'm running tomcat-6.0.35.A on unix rhel5.
我在 unix rhel5 上运行 tomcat-6.0.35.A。
回答by maximdim
I think I have seen which jars are open on startup in one of Tomcat log files. If not I could think about two possible alternatives:
我想我已经在 Tomcat 日志文件之一中看到了在启动时打开了哪些 jar。如果不是,我可以考虑两种可能的选择:
- Add
-verbose:class
toJAVA_OPTS
in Tomcat startup script. It should print classes as they're loaded by JVM (lots of output). Grep log file (orstdout
) to find if classes from your jar are listed - Use Linux lsofcommand to see files opened by the Tomcat process.
- 加入
-verbose:class
到JAVA_OPTS
在Tomcat启动脚本。它应该打印由 JVM 加载的类(大量输出)。Grep 日志文件(或stdout
)以查找是否列出了 jar 中的类 - 使用Linux lsof命令查看Tomcat 进程打开的文件。
回答by Eugene Kuleshov
Jars aren't really "loaded on startup". But Tomcat's system class loader is loading classes from those jars. You can check if some class from the jar is available either:
罐子并没有真正“在启动时加载”。但是 Tomcat 的系统类加载器正在从这些 jar 加载类。您可以检查 jar 中的某个类是否可用:
- by trying to load that class from your code, e.g.
getClass().getClassloader().loadClass(className)
- by trying to load class as a resource,
e.g.
getClass().getResource("/" + className.replace('.', '/') + ".class")
- 通过尝试从您的代码加载该类,例如
getClass().getClassloader().loadClass(className)
- 通过尝试将类加载为资源,例如
getClass().getResource("/" + className.replace('.', '/') + ".class")
In second case you should have name or your jar file in the url returned by getResource()
.
在第二种情况下,您应该在getResource()
.