classloader

How to explore which classes are loaded from which JARs?

a 夏天 提交于 2019-11-26 19:15:27
问题 Is there a way to determine which classes are loaded from which jars at runtime? I'm sure we've all been in JAR hell before. I've run across this problem a lot troubleshooting ClassNotFoundException s and NoClassDefFoundError s on projects. I'd like to avoid finding all instances of a class in jars and using process of elimination on the code causing a CNFE to find the culprit. Will any profiling or management tools give you this kind of information? This problem is super annoying purely

java classloader 过程

人走茶凉 提交于 2019-11-26 18:55:59
jvm classLoader architecture : a, Bootstrap ClassLoader/启动类加载器 主要负责jdk_home/lib目录下的核心 api 或 -Xbootclasspath 选项指定的jar包装入工作. b, Extension ClassLoader/扩展类加载器 主要负责jdk_home/lib/ext目录下的jar包或 -Djava.ext.dirs 指定目录下的jar包装入工作 c, System ClassLoader/系统类加载器 (AppClassLoader ?) 主要负责java -classpath/-Djava.class.path所指的目录下的类与jar包装入工作. b, User Custom ClassLoader/用户自定义类加载器(java.lang.ClassLoader的子类) 在程序运行期间, 通过java.lang.ClassLoader的子类动态加载class文件, 体现java动态实时类装入特性. 类加载器的特性: 1, 每个ClassLoader都维护了一份自己的名称空间, 同一个名称空间里不能出现两个同名的类。 2, 为了实现java安全沙箱模型顶层的类加载器安全机制, java默认采用了 ” 双亲委派的加载链 ” 结构. 如下图: 类图: 类图中,

How to use ClassLoader.getResources() correctly? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-11-26 18:38:10
This question already has an answer here: How to list the files inside a JAR file? 13 answers How can I use ClassLoader.getResources() to find recursivly resources from my classpath? E.g. finding all resources in the META-INF "directory": Imagine something like getClass().getClassLoader().getResources("META-INF") Unfortunately, this does only retrieve an URL to exactly this "directory". all resources named bla.xml (recursivly) getClass().getClassLoader().getResources("bla.xml") But this returns an empty Enumeration . And as a bonus question: How does ClassLoader.getResources() differ from

Java 8 ScriptEngine across ClassLoaders

人盡茶涼 提交于 2019-11-26 18:30:05
问题 I need to execute some javascript code 'inside' different classloaders. If it is java, each task will run in separate class loader. Now I need this to be javascript. Do I need to create new instance of ScriptEngine in each classloader, or is it ok to share one across class loaders? 回答1: From your question it is not clear why'd you look for such classloader isolation. So, I'm summarizing nashorn's classloader here - may be, you'll get what you're looking for. Nashorn and classloaders: Nashorn

Solution for the ClassCastException due to ClassLoader issue

北战南征 提交于 2019-11-26 18:29:14
问题 I have two ClassLoaders which loads the same class. So, obviously these can't cast to one another. But I need to access an object created in the other ClassLoader. I have access to both ClassLoaders. How can I use that object in the other class? I don't need to cast the object to match to the current ClassLoader. But the issue is that the returned object's type is Object . So, I have to cast down that object to access some methods. How can I do that? Normal cast like the following causes

Java ServiceLoader with multiple Classloaders

夙愿已清 提交于 2019-11-26 17:55:21
问题 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

Flow of class loading for a simple program

旧街凉风 提交于 2019-11-26 17:54:08
问题 I am just now beginning to learn the internal architecture of Java. I have roughly understood the concept of class loading which loads the required classes when jvm runs, ClassNotFoundException is thrown when a class is not found and specific class loader loads the classes referenced by the class. Can someone please explain clearly the flow of class loading i.e. the sequence of bootstrap class loading and user-defined class loading in the sample Java code below. import java.io.File; public

How can I list all classes loaded in a specific class loader

心不动则不痛 提交于 2019-11-26 17:37:04
For debug reasons, and curiosity, I wish to list all classes loaded to a specific class loader. Seeing as most methods of a class loader are protected, what is the best way to accomplish what I want? Thanks! finnw Instrumentation.getInitiatedClasses(ClassLoader) may do what you want. According to the docs: Returns an array of all classes for which loader is an initiating loader. I'm not sure what "initiating loader" means though. If that does not give the right result try using the getAllLoadedClasses() method and manually filtering by ClassLoader. How to get an instance of Instrumentation

Java - how to load different versions of the same class?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 17:28:40
I have read a lot about Java classloaders, but so far I have failed to find an answer for this simple question: I have two versions of com.abc.Hello.class in jars v1.jar and v2.jar . I want to use both in my application. What is the simplest way of doing this ? I don't expect to be that simple, but something along these lines would be awesome : Classloader myClassLoader = [magic that includes v1.jar and ignores v2.jar] Hello hello = myclassLoader.load[com.abc.Hello] And in a different class : Classloader myClassLoader = [magic that includes v2.jar and ignores v1.jar] Hello hello =

Load properties file in JAR?

萝らか妹 提交于 2019-11-26 17:10:05
I'm having trouble when one of the jars that my web app depends on tries to load a properties file from within the jar. Here is the code in the jar. static { Properties props = new Properties(); try { props.load(ClassLoader.getSystemResourceAsStream("someProps.properties")); } catch (IOException e) { e.printStackTrace(); } someProperty = props.getProperty("someKey"); } The properties file is in my "src/main/resources" directory of the Maven project. When I run this code from my junit test in Eclipse, it executes just fine. When the project is built with Maven into a jar, and included as a