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
24
votes
10 answers

How to determine main class at runtime in threaded java application?

I want to determine the class name where my application started, the one with the main() method, at runtime, but I'm in another thread and my stacktrace doesn't go all the way back to the original class. I've searched System properties and…
Erik R.
  • 7,152
  • 1
  • 29
  • 39
24
votes
12 answers

What reasons have people had to write their own classloader

I was recently asked in an interview about the order in which classloaders are called when a class is loaded. Unfortunately I've never had the need to write my own classloader so at the time was unfamiliar with the intricacies of classloading. This…
Brett Hannah
  • 4,307
  • 4
  • 30
  • 33
24
votes
9 answers

Difference between Loading a class using ClassLoader and Class.forName

Below are 2 code snippets The first one uses ClassLoader class to load a specified class ClassLoader cls = ClassLoader.getSystemClassLoader(); Class someClass = cls.loadClass("TargetClass"); The second one uses Class.forName() to load a specified…
Ebbu Abraham
  • 2,036
  • 5
  • 22
  • 30
24
votes
5 answers

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

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…
sivabudh
  • 31,807
  • 63
  • 162
  • 228
23
votes
6 answers

How to replace classes in a running application in java ?

Say I have a class named NameGenerator. I can use this to generate names according to a given logic. Then I write a TestNameGeneration class with a method that asks for a letter from the user and generate a name in accordance. Now I want to change…
Prasad Weera
  • 1,223
  • 3
  • 13
  • 25
22
votes
1 answer

How does clojure class reloading work?

I've been reading code and documentation to try to understand how class reloading works in clojure. According to many websites, such as http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html , whenever you load a class…
bmillare
  • 4,183
  • 2
  • 23
  • 42
22
votes
8 answers

Is the Java classpath final after JVM startup?

I have read a lot about the Java class loading process lately. Often I came across texts that claimed that it is not possible to add classes to the classpath during runtime and load them without class loader hackery (URLClassLoaders etc.) As far as…
Jens
  • 20,533
  • 11
  • 60
  • 86
22
votes
2 answers

Declaring variable final and static

This comment was made in a code review and the person who made it is no longer on our team. Any type that must be resolved by the classloader at runtime should never have instances which are held by references declared to be both final and…
sdoca
  • 7,832
  • 23
  • 70
  • 127
22
votes
2 answers

Replace content of some methods at runtime

I would like to replace the content of some methods at runtime. I know I can use javassist for this but it does not work because the classes I would like to enhance are already loaded by the system classLoader. How can I do, to replace the content…
Fabien Henon
  • 783
  • 15
  • 37
21
votes
3 answers

When does the JVM load classes?

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…
Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
21
votes
2 answers

Classloaders hierarchy in Java 9

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 work?
Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
21
votes
8 answers

No suitable classloader found for grab

I have this at the beginning of a class: @Grab(group = 'org.ccil.cowan.tagsoup', module = 'tagsoup', version = '1.2') class MyClass{... I'm trying to unit test this class, but whenever I try to run JUnit 4 tests, I get this error: Caused by:…
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
21
votes
6 answers

ThreadLocal garbage collection

From javadoc Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are subject…
michael nesterenko
  • 14,222
  • 25
  • 114
  • 182
21
votes
2 answers

How do Spring annotations work?

Motivation As a follow-up to my previous questions on classloading How is the Classloader for a class chosen? How Classloader determines which classes it can load? Where does bytecode injection happen? I'm curious about how do annotations work in…
ipavlic
  • 4,906
  • 10
  • 40
  • 77
21
votes
2 answers

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

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…
caw
  • 30,999
  • 61
  • 181
  • 291