classloader

Java: How to load Class stored as byte[] into the JVM?

[亡魂溺海] 提交于 2019-11-27 12:06:53
If one has serialized the entire .class file into byte[], and assuming the name of the class is known (passed along with the byte[]), how do you convert byte[] -> Class -> then load it to the JVM so that I could later use it by calling the Class.forName()? NOTE: I'm doing this because I sent the .class over to another host, and the host's JVM doesn't know about this .class. I'm actually using something like this right now in a test to give a set of Class definitions as byte[] to a ClassLoader: public static class ByteClassLoader extends URLClassLoader { private final Map<String, byte[]>

Jar hell: how to use a classloader to replace one jar library version with another at runtime

痞子三分冷 提交于 2019-11-27 12:00:31
问题 I'm still relatively new to Java, so please bear with me. My issue is that my Java application depends on two libraries. Let's call them Library 1 and Library 2. Both of these libraries share a mutual dependency on Library 3. However: Library 1 requires exactly version 1 of Library 3. Library 2 requires exactly version 2 of Library 3. This is exactly the definition of JAR hell (or at least one its variations). As stated in the link, I can't load both versions of the third library in the same

Static references are cleared--does Android unload classes at runtime if unused?

时间秒杀一切 提交于 2019-11-27 11:55:40
I have a question specific to how the classloading / garbage collection works in Android. We have stumbled upon this issue a few times now, and as far as I can tell, Android behaves different here from an ordinary JVM. The problem is this: We're currently trying to cut down on singleton classes in the app in favor of a single root factory singleton which sole purpose is to manage other manager classes. A top level manager if you will. This makes it easy for us to replace implementations in tests without opting for a full DI solution, since all Activities and Services share the same reference

Check if class exists in Java classpath without running its static initializer?

自作多情 提交于 2019-11-27 11:52:38
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? André Try the forName(String name, boolean initialize, ClassLoader loader) method of Class and set the param initialize to false . JavaDoc link 来源: https://stackoverflow.com/questions/3466568/check-if-class-exists-in-java-classpath-without-running-its-static

Is there a way to get which classes a ClassLoader has loaded?

雨燕双飞 提交于 2019-11-27 11:51:00
I am trying to implement some unit testing for an old framework. I am attempting to mock out the database layer. Unfortunately our framework is a bit old and not quite using best practices so there is no clear separation of concerns. I am bit worried that trying to mock out the database layer might make the JVM load a huge number of classes that won't even be used. I don't really understand class loaders that well so this might not be a problem. Is there a way to take a peak at all the classes a particular ClassLoader has loaded to prove what is going on under the hood? Be warned that using

Classloader issues - How to determine which library versions (jar-files) are loaded

拜拜、爱过 提交于 2019-11-27 11:35:03
问题 I've just solved another *I-though-I-was-using-this-version-of-a-library-but-apparently-my-app-server-has-already-loaded-an-older-version-of-this-library-*issue (sigh). Does anybody know a good way to verify (or monitor) whether your application has access to all the appropriate jar-files, or loaded class-versions? Thanks in advance! [P.S. A very good reason to start using the OSGi module architecture in my view!] Update : This article helped as well! It gave me insight which classes JBoss'

Java security: Sandboxing plugins loaded via URLClassLoader

倖福魔咒の 提交于 2019-11-27 11:28:25
Question summary: How do I modify the code below so that untrusted, dynamically-loaded code runs in a security sandbox while the rest of the application remains unrestricted? Why doesn't URLClassLoader just handle it like it says it does? EDIT: Updated to respond to Ani B. EDIT 2: Added updated PluginSecurityManager. My application has a plug-in mechanism where a third party can provide a JAR containing a class which implements a particular interface. Using URLClassLoader, I am able to load that class and instantiate it, no problem. Because the code is potentially untrusted, I need to prevent

Java 加载资源文件

自闭症网瘾萝莉.ら 提交于 2019-11-27 11:15:06
简介 Java中获取资源的最常用的2中方式就是使用Class的getResource和使用ClassLoader的getResource方法,当然还有它们相关的方法。这里就介绍一下使用这2中方式的区别,和它们搜索使用的路径。 这里先说结论(hotspot): ClassLoader的getResource(name)方法会依次查找: 在"sun.boot.class.path"指定的路径问根目录下查找name资源 在"java.ext.dirs"指定的路径为根目录下查找name资源 在"java.class.path"指定的路径为根目录下查找name资源 利用ClassLoader(自定义的,重写了findResource)的findResource(name)获取URL Class的getResource(name)方法是调用的ClassLoader的getResource(name)方法,但是它做了2点处理: 如果name以"/"开头,就把name中开头的"/"去掉,然后调优ClassLoader的getResource(name)方法。然后在ClassLoader的getResource(name)方法搜索方式搜索。 如果name不以"/"开头,那么就用Class的包名+name作为新的name来调用ClassLoader的getResource(name)方法。cn

What are the differences between the various Java plugins for hot class reloading and which one is the most intuitive?

时间秒杀一切 提交于 2019-11-27 10:21:01
问题 I am currently trying to implement hot class reloading in a Java application, however there are so many plugins to choose from and I cannot find a good comparison between the options. Also the websites of the plugins are not all very clear on what the exact features are and how to use them. There is also the option of making a custom hot class reloading ClassLoader , but I feel like that is similar to "reinventing the wheel" if there are already so many plugins which can do the job.. do other

Java ServiceLoader with multiple Classloaders

主宰稳场 提交于 2019-11-27 10:14:52
What are the best practices for using ServiceLoader in an Environment with multiple ClassLoaders? The documentation recommends to create and save a single service instance at initialization: private static ServiceLoader<CodecSet> codecSetLoader = ServiceLoader.load(CodecSet.class); This would initialize the ServiceLoader using the current context classloader. Now suppose this snippet is contained in a class loaded using a shared classloader in a web container and multiple web applications want to define their own service implementations. These would not get picked up in the above code, it