classloader

Java classloaders: why search the parent classloader first?

若如初见. 提交于 2019-12-03 08:04:31
问题 The correct behaviour for a classloader in Java is to: If it has already been loaded, return the class Call the parent loadClass() Try and load the class itself. So the class defined in the system classpath should always get loaded first. Tomcat defines classloader per war, which has the system classloader as a parent, so if you try to load a class, it will first look in the system classpath and then in the classpath defined in the war file. As per my understanding, this is for two reasons:

What happens when java program starts?

蹲街弑〆低调 提交于 2019-12-03 07:45:47
问题 Recently have been touched Java classloaders and suddenly recognized that do not fully understand what happens step-by-step when someone calls java -jar App.jar Well I guess a new instance of JVM is created it uses ClassLoader to load main class and other classes byte-code is started to execute from main() method But still I suppose there are many things I need to know more about it. Who and how decides which classes should be loaded at startup and which once needed? I have found two related

How to determine absolute path (if any) of a loaded Class?

假装没事ソ 提交于 2019-12-03 07:34:35
问题 Is there a (compatible, if possible) way to determine the absolute path of a loaded Class? Of course, this is not always possible (if you think of dynamically created classes), but if the loaded Class is inside a jar how to get the absolute path for this jar? 回答1: MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath() Fullcode: package org.life.java.so.questions; /** * * @author jigar */ public class GetClassPath { public static void main(String[] args) { System.out

How JVM starts looking for classes?

不羁的心 提交于 2019-12-03 07:33:01
问题 I was curious about what all locations JVM looks for executing a program? I'm more interested in understanding in what sequence and where does JVM look for class files, like does it look into java libs, extension libs, classpath any directory like the current directory from where the java is invoked? I'm more interested in JVM behaviour and not how class loader load class, which I know has parent delegation mechanism till root. If a class is executed from directory where the compiled class is

How can I safely solve this Java context classloader problem?

南楼画角 提交于 2019-12-03 07:22:56
One and only one of my hundreds of users has trouble starting my Java desktop app. It only starts for him about one-third of the time. The other two-thirds of the time a NullPointerException is thrown at startup: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.util.Hashtable.put(Hashtable.java:394) at javax.swing.JEditorPane.registerEditorKitForContentType(JEditorPane.java:1327) at javax.swing.JEditorPane.registerEditorKitForContentType(JEditorPane.java:1309) at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(JEditorPane.java:1387) at javax.swing.JEditorPane

What do people use class loading for?

◇◆丶佛笑我妖孽 提交于 2019-12-03 07:10:06
问题 So, every Java text book talks about how flexible Java is since it can load classes at run time. Just cobble together a string and give it to Class.forName() , and catch the ClassNotFoundException and handle it. So much for the theory. Can you give examples of how you've been using Java class loading to achieve a feature which otherwise wouldn't have been possible or easy? Note that I'm not asking "what great things could we do?" - I'm looking for real-world examples, be it an open-source

What does the sun.reflect.CallerSensitive annotation mean?

﹥>﹥吖頭↗ 提交于 2019-12-03 06:34:45
问题 What is implied by @CallerSensitive annotation above methods? For example,the annotation is present in getClassLoader method of Class @CallerSensitive public ClassLoader getClassLoader() { // } 回答1: According to the JEP I linked to in the comments (also here), A caller-sensitive method varies its behavior according to the class of its immediate caller. It discovers its caller’s class by invoking the sun.reflect.Reflection.getCallerClass method. If you look at the implementation of Class

Java's classloader versus jars-within-jars

邮差的信 提交于 2019-12-03 06:18:10
We have an executable JAR file that sometimes contains other JAR files. (The whole thing rests on four other downloaded JARs, riding on the back of a giant deployed turtle in space.) At runtime, we dynamically load that nested JAR file doing the following: // wearyingly verbose error handling elided URL nestedURL = the_main_system_classloader.getResource("path/to/nested.jar"); File temp = File.createTempFile (....); // copy out nestedURL contents into temp, byte for byte URL tempURL = temp.toURI().toURL(); URLClassLoader loader = URLClassLoader.newInstance(new URL[]{ tempURL }); Class<?> clazz

Programmatically fill the JVM Permanent Generation (PermGen) memory region

只谈情不闲聊 提交于 2019-12-03 06:13:51
问题 I need to test some JMX monitoring scripts I have developed, In particular I would like to verify that my monitoring of the PermGen region is working. So in order to test this I would like to be able to run a bit of code that loads a significant number of classes in order to consume PermGen. My current plan is to write a script to generate prefix(1..n).java compile them and then on cue run: for( int i=1 ; i < n ; i ++){ Class.forName("com.mypackage.prefix"+i); } Is there a more elegant

Java ClassLoader delegation model?

。_饼干妹妹 提交于 2019-12-03 06:04:37
问题 When calling loadClass() on a ClassLoader, does the ClassLoader first check if the class has been loaded, or does it immediately delegate this check to its parent ClassLoader ? Java API says: When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. But there's a specific chapter about class loader in the book Java Reflection in Action that says: Class