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
21
votes
5 answers

What purpose does Class.forName() serve if you don't use the return value?

I've seen this line in a sample application for using a commercial JDBC driver: Class.forName("name.of.a.jcdb.driver") The return value is not used. What purpose does this line serve?
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
20
votes
3 answers

What does "new" do in Java w.r.t. class loader?

I cannot easily find it in JLS/JVMSpec, nor in SO. I'm sure it must've been asked... So, what does "new" do actually? Suppose we instantiate a class B in A: class A { // ... new B(); // ... } Is this equivalent to class A { // ... …
MaDa
  • 10,511
  • 9
  • 46
  • 84
20
votes
2 answers

What for Sun JVM creates instances of sun.reflect.DelegatingClassLoader at runtime?

While analyzing a heap dump using jhat I have observed many instances of DelegatingClassLoader created although they were not called explicitly in code. I expect this to be some sort of reflection optimization mechanism. Does anybody know the…
Eugen
  • 2,292
  • 3
  • 29
  • 43
20
votes
2 answers

Singleton class with several different classloaders

E.g I have class Singleton with static field instance: public class Singleton { private static Singleton instance; // other code, construct, getters, no matter } I can load this class twice with two different classloaders. How could I…
lies
  • 361
  • 2
  • 4
  • 9
19
votes
6 answers

Java classloaders: why search the parent classloader first?

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…
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
19
votes
8 answers

Java 9, compatability issue with ClassLoader.getSystemClassLoader

The following code adds jar file to the build path, it works fine with Java 8. However, it throws exception with Java 9, the exception is related to the cast to URLClassLoader. Any ideas how this can be solved? an optimal solution will edit it to…
Mostafa abdo
  • 616
  • 1
  • 8
  • 21
19
votes
3 answers

copying a jar file in bootstrap class path not working

A have a Java code as: public class Hello { public void print() { System.out.println("Hi"); } } I compiled it and created a Hello.class. I added it to a Jar file hello.jar as: $jar -cvf hello.jar…
my name is GYAN
  • 1,269
  • 13
  • 27
19
votes
2 answers

How to get Java File absolute path from InputStream?

I'm on Java 6 and I have a method that scans the runtime classpath for a file called config.xml. If found, I would like to read the contents of the file into a string: InputStream istream =…
user1768830
19
votes
5 answers

Java Classloader - how to reference different versions of a jar

This is a common problem. I'm using 2 libraries A.jar and B.jar and these depend on different versions of the same jar. Let's say that at runtime I need THIS.x.x.x.jar MY.jar -> A.jar -> THIS.1.0.0.jar -> B.jar -> C.jar ->…
mickthompson
  • 5,442
  • 11
  • 47
  • 59
19
votes
1 answer

Why Does Clojure Use the Context Classloader by Default?

Why is use-context-classloader set to true by default? Why doesn't Clojure use the current class loader?
mudgen
  • 7,213
  • 11
  • 46
  • 46
18
votes
3 answers

What does "When a Class is loaded" actually mean?

It is said that static blocks in java run only once when that class is loaded. But what does it actually mean? At which point is a class loaded by JVM (Java Virtual Machine)? Is it when the main method in that class is called? And is it that all the…
whitehat
  • 2,381
  • 8
  • 34
  • 47
18
votes
2 answers

Run each JUnit Test with a Separate ClassLoader (no, really)

How can I have JUnit use a separate ClassLoader for each test class it executes? I am writing a JUnit TestRunner for a library that sets a lot of static variables. I essentially want to reset all of these between each test class, without needing to…
DeejUK
  • 12,891
  • 19
  • 89
  • 169
18
votes
2 answers

Android ClassLoader memory leak

Motivation: I am using some native libraries in my Android application and I want to unload them from the memory at some point in time. Libraries get unloaded when ClassLoader that loaded class that loaded native libraries gets garbage collected.…
ph4r05
  • 1,826
  • 1
  • 18
  • 15
18
votes
2 answers

Applet served by Java Web Start, resources requested to WEB Server before look in the JAR files

I am new here and I apologize for my bad English. I have a little problem with an Applet class served by Java Web Start technology. I have some platform dependent JAR files which Web Start download correctly, but when I get the content by…
Roberto Santini
  • 216
  • 2
  • 7
18
votes
4 answers

How to get classloader for a bundle in equinox?

I have read a lot of equinox code for this, but still can't figure out a non-hacky way of getting the classloader for a osgi bundle in eclipse equinox setup. Is there one?
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94