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
812
votes
11 answers

Dealing with "Xerces hell" in Java/Maven?

In my office, the mere mention of the word Xerces is enough to incite murderous rage from developers. A cursory glance at the other Xerces questions on SO seem to indicate that almost all Maven users are "touched" by this problem at some point. …
Justin Garrick
  • 14,767
  • 7
  • 41
  • 66
375
votes
20 answers

How to load JAR files dynamically at Runtime?

Why is it so hard to do this in Java? If you want to have any kind of module system you need to be able to load JAR files dynamically. I'm told there's a way of doing it by writing your own ClassLoader, but that's a lot of work for something that…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
302
votes
4 answers

Difference between thread's context class loader and normal classloader

What is the difference between a thread's context class loader and a normal class loader? That is, if Thread.currentThread().getContextClassLoader() and getClass().getClassLoader() return different class loader objects, which one will be used?
abracadabra
  • 3,067
  • 3
  • 17
  • 7
294
votes
13 answers

Scanning Java annotations at runtime

How do I search the whole classpath for an annotated class? I'm doing a library and I want to allow the users to annotate their classes, so when the Web application starts I need to scan the whole classpath for certain annotation. I'm thinking about…
Alotor
  • 7,407
  • 12
  • 38
  • 36
230
votes
9 answers

What is the difference between Class.getResource() and ClassLoader.getResource()?

I wonder what the difference is between Class.getResource() and ClassLoader.getResource()? edit: I especially want to know if any caching is involved on file/directory level. As in "are directory listings cached in the Class version?" AFAIK the…
oligofren
  • 20,744
  • 16
  • 93
  • 180
215
votes
14 answers

URL to load resources from the classpath in Java

In Java, you can load all kinds of resources using the same API but with different URL protocols: file:///tmp.txt http://127.0.0.1:8080/a.properties jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class This nicely decouples the actual loading of…
Thilo
  • 257,207
  • 101
  • 511
  • 656
215
votes
12 answers

Find where java class is loaded from

Does anyone know how to programmaticly find out where the java classloader actually loads the class from? I often work on large projects where the classpath gets very long and manual searching is not really an option. I recently had a problem…
luke
  • 14,518
  • 4
  • 46
  • 57
195
votes
7 answers

Unloading classes in java?

I have a custom class loader so that a desktop application can dynamically start loading classes from an AppServer I need to talk to. We did this since the amount of jars that are required to do this are ridiculous (if we wanted to ship them). We…
el_eduardo
  • 3,138
  • 4
  • 21
  • 17
187
votes
3 answers

What does JVM flag CMSClassUnloadingEnabled actually do?

I cannot for the life of me find a definition of what the Java VM flag CMSClassUnloadingEnabled actually does, other than some very fuzzy high-level definitions such as "gets rid of your PermGen problems" (which it doesn't, btw). I have looked on…
Rich
  • 15,602
  • 15
  • 79
  • 126
185
votes
5 answers

Determine which JAR file a class is from

I am not in front of an IDE right now, just looking at the API specs. CodeSource src = MyClass.class.getProtectionDomain().getCodeSource(); if (src != null) { URL jar = src.getLocation(); } I want to determine which JAR file a class is from. Is…
Walter White
183
votes
8 answers

What is a Java ClassLoader?

In a few simple sentences, what is a Java ClassLoader, when is it used and why? OK, I read a wiki article. ClassLoader loads classes. OK. So if I include jar files and import, a ClassLoader does the job. Why should I bother with this ClassLoader?…
EugeneP
  • 11,783
  • 32
  • 96
  • 142
182
votes
15 answers

How to read a text file from resources in Kotlin?

I want to write a Spek test in Kotlin. How to read an HTML file from the src/test/resources folder? class MySpec : Spek( { describe("blah blah") { given("blah blah") { var fileContent: String = "" …
Olaf
  • 3,786
  • 4
  • 25
  • 38
175
votes
12 answers

How to avoid "Sharing is only supported for boot loader classes because bootstrap classpath has been appended" warning during debug with Java 11?

Recently I switched to the Java 11 and start to debug my app and saw this message: OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended Found only this commit and…
Dmitriy Dumanskiy
  • 11,657
  • 9
  • 37
  • 57
150
votes
13 answers

How to get names of classes inside a jar file?

I have a JAR file and I need to get the name of all classes inside this JAR file. How can I do that? I googled it and saw something about JarFile or Java ClassLoader but I have no idea how to do it.
sticksu
  • 3,660
  • 3
  • 23
  • 41
135
votes
5 answers

Java, Classpath, Classloading => Multiple Versions of the same jar/project

I know this may be a silly question for experienced coders. But I have a library (an http client) that some of the other frameworks/jars used in my project require. But all of them require different major versions like: httpclient-v1.jar => Required…
jens
  • 2,319
  • 4
  • 18
  • 11
1
2 3
99 100