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
36
votes
2 answers

What does the sun.reflect.CallerSensitive annotation mean?

What is implied by @CallerSensitive annotation above methods? For example,the annotation is present in getClassLoader method of Class @CallerSensitive public ClassLoader getClassLoader() { // }
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
36
votes
4 answers

How is the Java Bootstrap Classloader loaded?

In Java it is said that all the classes are being loaded by classloaders. So first of all, bootstrap classloader loads all the rt.jar classes. I am still confused as Classloader is also a class, so who will load this BootStrapClassloader?
Sunny Gupta
  • 6,929
  • 15
  • 52
  • 80
35
votes
9 answers

Updating a JAR whilst running

Given a jar runs within a JVM would it be possible to unload the current running Jar and remove it from the system. Download a new version and rename it with the same name of the last Jar and then initialise the new Jar, creating a seamless update…
James Moore
  • 3,417
  • 4
  • 20
  • 18
35
votes
1 answer

How to put custom ClassLoader to use?

Hello all and thanks for the attention! I have a problem that must both be easy and obvious, yet I am stuck. I want to deliver dynamically created Java classes to be used by a 3rd party library via a custom ClassLoader. Now my problem is: How do I…
roesslerj
  • 2,611
  • 5
  • 30
  • 44
35
votes
3 answers

sysLoader.getResource() problem in java

I am having following lines of code. sysLoader = (URLClassLoader)Thread.currentThread().getContextClassLoader(); url = sysLoader.getResource("tempFile.txt"); It is giving an weird problem. If I run this from a path where there is no space in the…
Vishal
  • 2,711
  • 7
  • 33
  • 42
34
votes
4 answers

CompletableFuture / ForkJoinPool Set Class Loader

I tackled down a very specific problem, whose solution seems to be something basic: My (Spring) application's classloader hierarchy is something like this: SystemClassLoader -> PlatformClassLoader -> AppClassLoader If I use Java CompleteableFuture…
Maurice Müller
  • 1,312
  • 3
  • 16
  • 32
34
votes
3 answers

Java security: Sandboxing plugins loaded via URLClassLoader

Question summary: How do I modify the code below so that untrusted, dynamically-loaded code runs in a security sandbox while the rest of the application remains unrestricted? Why doesn't URLClassLoader just handle it like it says it does? EDIT:…
Robert J. Walker
  • 10,027
  • 5
  • 46
  • 65
34
votes
7 answers

How are object dependencies between static blocks resolved?

I recently came across this at work. While I am not sure it is really a good idea, I don't understand how static blocks are handled by the compiler. Here is an example: Consider that you have classes A and B: public class A { public final…
nsanglar
  • 1,632
  • 1
  • 14
  • 24
33
votes
3 answers

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

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…
CodeClimber
  • 4,584
  • 8
  • 46
  • 55
33
votes
1 answer

How to load a Java class dynamically on android/dalvik?

I'm wondering if and how one can load dex or class files dynamically in dalvik, some quick'n'dirty test function I wrote was this: public void testLoader() { InputStream in; int len; byte[] data = new…
anselm
  • 792
  • 1
  • 8
  • 21
33
votes
6 answers

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

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), …
Sbodd
  • 11,279
  • 6
  • 41
  • 42
32
votes
5 answers

Jar hell: how to use a classloader to replace one jar library version with another at runtime

I'm still relatively new to Java, so please bear with me. My issue is that my Java application depends on two libraries. Let's call them Library 1 and Library 2. Both of these libraries share a mutual dependency on Library 3. However: Library 1…
Lukas S.
  • 5,698
  • 5
  • 35
  • 50
32
votes
6 answers

cast across classloader?

How can I do this: class Foo { public static Foo get() throws Exception { ClassLoader cl = new URLClassLoader(new URL[]{"foo.jar"}, null); // Foo.class is in foo.jar return (Foo)cl.loadClass("Foo").newInstance(); // fails on class cast …
IttayD
  • 28,271
  • 28
  • 124
  • 178
32
votes
4 answers

What loads the Java system classloader?

We know that we can override the system classloader with: java -Djava.system.class.loader=com.test.MyClassLoader xxx Then, since com.test.MyClassLoader itself is a class, by whom is it loaded? How do we get the class file of this "meta"…
James.Xu
  • 8,249
  • 5
  • 25
  • 36
31
votes
5 answers

How to explore which classes are loaded from which JARs?

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 ClassNotFoundExceptions and NoClassDefFoundErrors on projects. I'd like…
cwash
  • 4,185
  • 5
  • 43
  • 53