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

How to get classpath from classloader?

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…
Marcus Mathioudakis
  • 837
  • 1
  • 6
  • 19
52
votes
4 answers

How to find which jars and in what order are loaded by a classloader?

I could not find a clear answer to this question elsewhere, so I'll try here: Is there some way (programmatic or other) to get a list of JARs/classes loaded by an Application Classloader in the precise order they were loaded? By Application…
Marina
  • 3,894
  • 9
  • 34
  • 41
49
votes
5 answers

In Java, is it possible to know whether a class has already been loaded?

Is it possible to know whether a Java class has been loaded, without attempting to load it? Class.forName attempts to load the class, but I don't want this side effect. Is there another way? (I don't want to override the class loader. I'm looking…
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
48
votes
2 answers

How to safely access the URLs of all resource files in the classpath in Java 9+?

We learned from the release notes of Java 9 that The application class loader is no longer an instance of java.net.URLClassLoader (an implementation detail that was never specified in previous releases). Code that assumes that…
rzo1
  • 5,561
  • 3
  • 25
  • 64
48
votes
2 answers

How can I list all classes loaded in a specific class loader

For debug reasons, and curiosity, I wish to list all classes loaded to a specific class loader. Seeing as most methods of a class loader are protected, what is the best way to accomplish what I want? Thanks!
Yaneeve
  • 4,751
  • 10
  • 49
  • 87
47
votes
9 answers

getClass().getClassLoader() is null, why?

I've got some code that calls.. x = getClass().getClassLoader(); This returns null though. When I start the same code not from Eclipse, but the command line, it returns a classloader. I can hack the code to do this... if…
jeff porter
  • 6,560
  • 13
  • 65
  • 123
47
votes
4 answers

Java - how to load different versions of the same class?

I have read a lot about Java classloaders, but so far I have failed to find an answer for this simple question: I have two versions of com.abc.Hello.class in jars v1.jar and v2.jar. I want to use both in my application. What is the simplest way of…
kms333
  • 3,147
  • 7
  • 31
  • 39
43
votes
5 answers

Java: Difference between Class.forName and ClassLoader.loadClass

What is the difference between Class.forName and ClassLoader.loadClass in the following codes: Class theClass = Class.forName("SomeImpl"); SomeImpl impl = (SomeImpl)theClass.newInstance(); and Class theClass =…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
43
votes
1 answer

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

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,…
onejigtwojig
  • 4,771
  • 9
  • 32
  • 35
42
votes
5 answers

Order of loading jar files from lib directory

Could anyone explain the order in which jar files are loaded from the lib directory within Tomcat? Is it alphabetically? Randomly? Or some other order?
Damien
  • 4,081
  • 12
  • 75
  • 126
40
votes
7 answers

How do I create a parent-last / child-first ClassLoader in Java, or How to override an old Xerces version that was already loaded in the parent CL?

I would like to create a parent-last / child-first class loader, e.g. a class loader that will look for classes in the child class loder first, and only then delegate to it's parent ClassLoader to search for classes. Clarification: I know now that…
Eran Medan
  • 44,555
  • 61
  • 184
  • 276
39
votes
1 answer

Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar

I understand from What is the difference between Class.getResource() and ClassLoader.getResource()? and from own code, that getClass().getResource("/path/image.png") is identical to getClass().getClassLoader().getResource("path/image.png") The…
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
38
votes
5 answers

Java ServiceLoader with multiple Classloaders

What are the best practices for using ServiceLoader in an Environment with multiple ClassLoaders? The documentation recommends to create and save a single service instance at initialization: private static ServiceLoader codecSetLoader =…
Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
38
votes
5 answers

Java : in what order are static final fields initialized?

Okay, so say I have a class that looks like this : public class SignupServlet extends HttpServlet { private static final Logger SERVLET_LOGGER=COMPANYLog.open(SignupServlet.class); private static final ExceptionMessageHandler handler = new…
sangfroid
  • 3,733
  • 11
  • 38
  • 42
38
votes
2 answers

How to diagnose a Java 8 metaspace leak?

I have a J2EE application with some interesting behavior ... the heap seems to behave well, growing and shrinking with garbage collections as expected over time. There is no appreciable overall long term heap expansion. However, the metaspace just…