在不运行其静态初始化程序的情况下检查 Java 类路径中是否存在类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3466568/
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 class exists in Java classpath without running its static initializer?
提问by Epaga
If I use
如果我使用
try {
Class.forName("my.package.Foo");
// it exists on the classpath
} catch(ClassNotFoundException e) {
// it does not exist on the classpath
}
the static initializer block of "Foo" is kicked off. Is there a way to determine whether a class "my.package.Foo" is on the classpath without kicking off its static initializer?
“Foo”的静态初始化块被启动。有没有办法在不启动静态初始化程序的情况下确定类“my.package.Foo”是否在类路径上?
采纳答案by André
Try the forName(String name, boolean initialize, ClassLoader loader)
method of Class
and set the param initialize
to false
.
尝试forName(String name, boolean initialize, ClassLoader loader)
方法Class
并将参数设置initialize
为false
。