是否有可能在运行时所指定的系统的ClassLoader加载的.class文件? [英] Is it possible to have the System ClassLoader load .class files specified at run time?

查看:293
本文介绍了是否有可能在运行时所指定的系统的ClassLoader加载的.class文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的分配一个静态分析工具,分析使用ASM库的Java字节code。之一的ASM的,我们使用需要的零件(或至少,似乎需要)阶级从类加载器加载的

I am writing a static analysis tool for an assignment, it analyses Java bytecode using the ASM library. One of the parts of ASM that we use requires (or at least, appears to require) that the class be loaded from the ClassLoader.

我们希望该工具将能够分析.class文件,而不需要他们的类路径上。我们已经在运行时加载从指定的目录中.classes和使用InputStream阅读。这是在大多数情况下的ASM可接受。有一些类,如 SimpleVerifier ,它试图尽管加载类。

We were hoping the tool would be able to analyse .class files without requiring them on the classpath. We already load the .classes from a specified directory at run time and read them in using an InputStream. This is acceptable for ASM in most cases. There are some classes, such as SimpleVerifier, which attempt to load the classes though.

是否有可能,这种情况下,登记要加载的.class文件,以便调用的Class.forName()将加载它们?还是有一个简单的方法来扩展类加载器允许这样做?

Is it possible, under this scenario, to register the .class files to be loaded so that calls to Class.forName() will load them? Or is there an easy way to extend the ClassLoader to allow this?


编辑:在的URLClassLoader 的信息是有益的。不幸的是,使用 Thread.currentThread()。setContextClassLoader()来者的一个实例并没有在这种情况下工作。该库code我打电话到使用它检索使用的getClass()实例的初始化加载器。getClassLoader()

the information on URLClassLoader was useful. Unfortunately, using Thread.currentThread().setContextClassLoader() to an instance of that didn't work in this scenario. The library code I'm calling into uses a loader it retrieves on instance initialisation using getClass().getClassLoader().

当我设置的URLClassLoader的类尚未初始化所以时间我猜contextClassLoader不会加载这个类。

By the time I set the URLClassLoader the class hasn't been initialised so I guess the contextClassLoader does not load that class.

我是否理解正确的反应?将使用的URLClassLoader来加载第三方类是一种可能性?

Have I understand the responses correctly? Would using the URLClassLoader to load the 3rd party class be a possibility?

推荐答案

差不多了。

如果您是在什么地方编译的类,你可以用一个的URLClassLoader <加载它们/ A>。然后,您可以设置此的ClassLoader是当前线程的类加载器:<一href=\"http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#setContextClassLoader%28java.lang.ClassLoader%29\">Thread.setContextClassLoader(ClassLoader)

If you have classes compiled somewhere, you can load them with a URLClassLoader. You can then set this ClassLoader to be the ClassLoader for the current Thread: Thread.setContextClassLoader(ClassLoader)

用户可以在获得当前线程上下文类加载器并使用它来访问类的定义。

Users can that get the current threads context class loader and use that to access the class definition.

这篇关于是否有可能在运行时所指定的系统的ClassLoader加载的.class文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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