在不运行其静态初始化程序的情况下检查 Java 类路径中是否存在类? [英] Check if class exists in Java classpath without running its static initializer?

查看:20
本文介绍了在不运行其静态初始化程序的情况下检查 Java 类路径中是否存在类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用

   try {
      Class.forName("my.package.Foo");
      // it exists on the classpath
   } catch(ClassNotFoundException e) {
      // it does not exist on the classpath
   }

Foo"的静态初始化块被启动.有没有办法在不启动静态初始化程序的情况下确定类my.package.Foo"是否在类路径上?

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?

推荐答案

尝试ClassforName(String name, boolean initialize, ClassLoader loader)方法并设置参数 initializefalse.

Try the forName(String name, boolean initialize, ClassLoader loader) method of Class and set the param initialize to false.

JavaDoc 链接

这篇关于在不运行其静态初始化程序的情况下检查 Java 类路径中是否存在类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆