classloader

How to change default class loader in Java?

人走茶凉 提交于 2019-11-27 01:54:11
问题 Let's say I have three classes, example.ClassA, example.ClassB, and example.ClassLoader. ClassA prints out HelloWorld and ClassB imports example.ClassA and invokes its main() method. If I do this: java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassA It works and uses my class loader. However, if I do this: java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassB ClassB uses my class loader, but ClassA (which was imported by ClassB) is

Java Enums: Two enum types, each containing references to each other?

爷,独闯天下 提交于 2019-11-27 01:33:14
Is there a way to get around the class-loading issues caused by having two enums that reference each other? I have two sets of enumerations, Foo and Bar, defined like so: public class EnumTest { public enum Foo { A(Bar.Alpha), B(Bar.Delta), C(Bar.Alpha); private Foo(Bar b) { this.b = b; } public final Bar b; } public enum Bar { Alpha(Foo.A), Beta(Foo.C), Delta(Foo.C); private Bar(Foo f) { this.f = f; } public final Foo f; } public static void main (String[] args) { for (Foo f: Foo.values()) { System.out.println(f + " bar " + f.b); } for (Bar b: Bar.values()) { System.out.println(b + " foo " +

When does the JVM load classes?

狂风中的少年 提交于 2019-11-27 01:13:14
问题 Assume I have the following class: class Caller { public void createSomething() { new Something(); } } Would executing this line: static void main() { Class<?> clazz = Caller.class; } cause the JVM to load the class Something or is the class loading deferred until the method createSomething() is called? 回答1: A class is loaded only when you require information about that class. public class SomethingCaller { public static Something something = null; // (1) does not cause class loading public

Classloaders hierarchy in Java 9

半世苍凉 提交于 2019-11-27 01:03:15
问题 As of Java-8, I know the hierarchy of the classloaders has been as follows:- Bootstrap classloader -> Extension classloader -> Application classloader What is the change in the hierarchy of classloaders in Java 9 and how does it works? 回答1: The ClassLoader as revised in Java-9 states that: The Java run-time has the following built-in class loaders: Bootstrap class loader : The virtual machine's built-in class loader typically represented as null, and does not have a parent. Platform class

When and how is a java classloader marked for garbage collection?

ぃ、小莉子 提交于 2019-11-27 00:34:23
问题 We are creating multiple child classloaders to load in multiple subapplications into a Java application "container", prototyping hot deployment. When the classpath of a particular classloader has changed (i.e. jars have been added, deleted, updated), the old classloader is thrown away (unreferenced) and a new classloader is created for the new classpath of jars. After updating the classpath, triggering the hot deployment, we took a heap dump. The heap dump (using Memory Analyzer) indicates

Android: “Class loader may fail for processes that host multiple applications”

廉价感情. 提交于 2019-11-27 00:30:02
问题 What does this message in Eclipse's logcat for Android mean? W/ActivityThread: ClassLoader.getResources: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader()); Unfortunately, there is no context given as to this warning, so I don't know what causes this problem and how I can resolve it. 回答1: Background

How to get classpath from classloader?

孤者浪人 提交于 2019-11-27 00:12:13
问题 I am using some third party code which when given a '-classpath' command line argument doesnt set the java.class.path, but instead just creates a classloader, adds all the urls for the items on the command line specified classpath to the classloader, and then sets it to be the context classloader. In a plugin class to this code that I have written, I get an instance of this classloader, and somehow need to use it to get back the underlying classpath, so that I can use it in an invocation of

How does class loading work when the same class exists in different applications on the same server?

删除回忆录丶 提交于 2019-11-26 23:57:46
问题 I have multiple web-apps running on an app server and each web-app WAR file contains a copy of the same jar file. Does this mean that a class in that jar file will be loaded multiple times in the JVM, once for each WAR file it exists in? Following on from that, if I have a static synchronized method in such a class, is it only synchronized among threads within the web-app it exists in but not synchronized against the same method in the same class in a different jar file in a different WAR

Class.forName() vs ClassLoader.loadClass() - which to use for dynamic loading? [duplicate]

冷暖自知 提交于 2019-11-26 23:35:35
This question already has an answer here: Difference between Loading a class using ClassLoader and Class.forName 9 answers When dynamically loading a class, when is it appropriate to use Class.forName("SomeClass"); and when should I use ClassLoader.getSystemClassLoader().loadClass("SomeClass"); Or, are they two ways of doing the same thing? Bruno Reis They are quite different! As stated in the documentation for Class.forName(String) , Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to: Class.forName(className, true,

log4j and the thread context classloader

六眼飞鱼酱① 提交于 2019-11-26 23:14:35
问题 I'm a newbie to Java and just starting to figure out the concept of class loaders. Right now I am having some issues with log4j regarding its use of the thread context classloader. I'm getting the following errors: A "org.apache.log4j.ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable. The class "org.apache.log4j.Appender" was loaded by [java.net.URLClassLoader@105691e] whereas object of type "org.apache.log4j.ConsoleAppender" was loaded by [sun.misc.Launcher