Questions tagged [classloader]

A class loader is an object that is responsible for loading classes in Java.

A class loader is an object that is responsible for loading classes in Java.

Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system. A class loader may also chose to delegate to another class loader. It may do this before or after doing a normal lookup.

Every Class object contains a reference to the class loader that defined it. This class loader is used by the runtime for linking to other classes referenced by this class. Together with the name of the class the defining class loader uniquely identifies a class. It's therefore possible to have several classes in with the same name in Java as long as they have a different defining class loader.

4126 questions
16
votes
3 answers

Is it always safe to call getClass() within the subclass constructor?

An article on classloading states that the method getClass() should not be called within a constructor because: object initialization will be complete only at the exit of the constructor code. The example they gave was: public class…
Pacerier
  • 86,231
  • 106
  • 366
  • 634
16
votes
4 answers

Why can't load inner class? ClassNotFoundException

Why does the following code cause ClassNotFoundException? public class App02 { public static class A { } public static void main(String[] args) throws ClassNotFoundException { try { …
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
16
votes
4 answers

How to unload an already loaded class in Java?

How to unload a class from the class loader, so that I can use the recently changed class on the fly without restarting my application (hot deployment)? Is it possible to do that?
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
16
votes
8 answers

How can I modify a java.lang class on the fly?

I'm looking for a way to add fields to an Thread on the fly by rewriting the byte code and reloading the class, not sure if it is at all possible. Any pointers welcome. I found some info on modifying and loading a class, and I know JRebel can…
16
votes
2 answers

Are static fields in Activity classes guaranteed to outlive a create/destroy cycle?

I frequently run into the problem that I have to preserve state between several invocations of an activity (i.e. going through several onCreate()/onDelete() cycles). Unfortunately, Android's support for doing that is really poor. As an easy way to…
mxk
  • 43,056
  • 28
  • 105
  • 132
16
votes
4 answers

URLClassLoader and accessibility of package-private methods

I have a class Formula, located in package javaapplication4, which I load with a URLClassLoader. However, when I call it from another class Test1, located in the same package, I can't access its methods that have a default access modifier (I can…
assylias
  • 321,522
  • 82
  • 660
  • 783
16
votes
1 answer

Override class in java

Assume I have a project K K depends lib.jar In lib.jar , there is a class named x.y.z.Foo If i create the same class x.y.z.Foo in K , then in this project when I create a instance of Foo , now will JVM use Foo in K rather than in lib.jar ? And if…
jackalope
  • 1,554
  • 3
  • 17
  • 37
15
votes
4 answers

why jvm has many clasloaders? why not one?

I am learning ClassLoader in Java, then I want to know, why JVM has many classloaders, why not only one? The one first load /lib, then load /lib/ext, and last load classpath. If you have custom classloader, the…
liuxiaori
  • 317
  • 2
  • 10
15
votes
2 answers

Why does a class containing a method call to a missing Interface within unused code cause a Java class loading error?

I'm seeing some class loading behavior that appears to be inconsistent with the JVM spec and am wondering if this is a bug. Or if not, hoping someone can explain why. The example code found below simply prints hello from its main method. It has an…
Eric Rosenberg
  • 1,543
  • 9
  • 18
15
votes
2 answers

Replacement System Classloader for Classes In Jars containing Jars

So far, the examples I have seen for custom ClassLoaders involve subclassing the URLClassLoader, and using that specific instance to load classes in resources. I have tried in vain to look for alternative methods to replace the SystemClassLoader, so…
Olaseni
  • 7,698
  • 16
  • 44
  • 68
15
votes
2 answers

Java EE class loading standard

WebSphere comes with parent last and parent first. Is this Java EE compliant? Is this supported by all application servers that are Java EE 5 compliant?
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
15
votes
2 answers

Difference between Thread.currentThread() classLoader and normal classLoader

Can you tell me what is the difference between Thread.currentThread().getContextClassLoader() and TestServlet.class.getClassLoader() do not mark it as duplicate and also please explain as well as provide me example when to use these Java…
sidhartha pani
  • 623
  • 2
  • 12
  • 23
15
votes
7 answers

Is Java class loader guaranteed to not load classes that aren't used?

Is there a guarantee that (the default, system) Java class loader doesn't attempt to load classes that aren't referred to in the code being run? A couple of examples of what I mean: I'm using a framework.jar which I know to contain references to…
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
15
votes
2 answers

Load DEX file dynamically on Android 5.0

Prior to Android 5.0 I was able to load DEX files dynamically using DexClassLoader and calling loadClass() method but with the latest Android version I get a ClassNotFoundException. Here is what I am doing: Generate DEX…
garibay
  • 1,046
  • 1
  • 10
  • 12
15
votes
7 answers

Java ClassLoader delegation model?

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…
Sawyer
  • 15,581
  • 27
  • 88
  • 124