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
30
votes
4 answers

Static references are cleared--does Android unload classes at runtime if unused?

I have a question specific to how the classloading / garbage collection works in Android. We have stumbled upon this issue a few times now, and as far as I can tell, Android behaves different here from an ordinary JVM. The problem is this: We're…
mxk
  • 43,056
  • 28
  • 105
  • 132
30
votes
1 answer

Kotlin: MyClass::class.java vs this.javaClass

I'm migrating a project to Kotlin, and this: public static Properties provideProperties(String propertiesFileName) { Properties properties = new Properties(); InputStream inputStream = null; try { inputStream =…
Matias Elorriaga
  • 8,880
  • 5
  • 40
  • 58
30
votes
5 answers

convert Class object to bytes

If I have a Class instance at runtime, can I get its byte[] representation? The bytes I'm interested in would be in the Class file format, such that they'd be valid input to [ClassLoader.defineClass][3]. [3]:…
Brian Harris
  • 2,735
  • 3
  • 22
  • 34
30
votes
3 answers

ClassLoader getResourceAsStream returns null

My project directory structure (in Eclipse): MyProject/ src/ --> "source directory" on Eclipse's classpath/buildpath com.me.myapp Driver myconfig.txt In Driver, I have the following code: public class…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
29
votes
3 answers

What is the use of Custom Class Loader

Recently I came accross the java custom class loader api. I found one use over here, kamranzafar's blog I am a bit new to the class loader concept. Can any one explain in detail, what are the different scenarios where we may need it or we should use…
Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82
28
votes
7 answers

Want to run non-threadsafe library in parallel - can it be done using multiple classloaders?

I work on a project where we use a library that is not guaranteed thread-safe (and isn't) and single-threaded in a Java 8 streams scenario, which works as expected. We would like to use parallel streams to get the low hanging scalability…
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
28
votes
6 answers

How can I implement an abstract singleton class in Java?

Here is my sample abstract singleton class: public abstract class A { protected static A instance; public static A getInstance() { return instance; } //...rest of my abstract methods... } And here is the concrete…
Simon
  • 563
  • 1
  • 6
  • 11
27
votes
5 answers

parallelStream() causing ClassNotFoundException with JAXB-API

In our application we sometimes get the following exception: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath. - with linked exception: [java.lang.ClassNotFoundException:…
Ethan Leroy
  • 15,804
  • 9
  • 41
  • 63
27
votes
5 answers

Does the Java ClassLoader load inner classes?

If I have a inner class declaration such as: Class A { public static class B { } } followed by: Class implClass = getClass().getClassLoader().loadClass("A"); Will the A$B inner class be loaded as well? What if the B inner class was not…
Janik Zikovsky
  • 3,086
  • 7
  • 26
  • 34
27
votes
5 answers

Classloader issues - How to determine which library versions (jar-files) are loaded

I've just solved another *I-though-I-was-using-this-version-of-a-library-but-apparently-my-app-server-has-already-loaded-an-older-version-of-this-library-*issue (sigh). Does anybody know a good way to verify (or monitor) whether your application has…
Johan Pelgrim
  • 6,033
  • 6
  • 37
  • 46
26
votes
4 answers

Where on the file system was my Java class loaded from?

I think this is a situation every Java programmer runs into if they do it long enough. You're doing some debugging and make a change to class. When you go to re-run the program, these changes don't seem to be picked up but rather the old class…
shsteimer
  • 28,436
  • 30
  • 79
  • 95
25
votes
8 answers

Java Class Loaders

Can anyone point me a good resource or explain me about the concept behind Class Loaders? I found the following resource on class loaders http://www.onjava.com/lpt/a/5586 but still no help. The following questions may look silly but trying to answer…
BlueGene
  • 1,071
  • 1
  • 20
  • 30
25
votes
2 answers

Where does the resolution stage of Java class loading actually start?

I just finished reading through the Java Virtual Machine Specification and the section on class loading left me puzzled. As far as I understood it in general and after reading the specification, I thought that the overall instantiation of a class…
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
25
votes
2 answers

What are the differences between the various Java plugins for hot class reloading and which one is the most intuitive?

I am currently trying to implement hot class reloading in a Java application, however there are so many plugins to choose from and I cannot find a good comparison between the options. Also the websites of the plugins are not all very clear on what…
PJvG
  • 1,310
  • 3
  • 16
  • 33
25
votes
4 answers

Java - loading annotated classes

I know there are incredible set of tools for loading plugin classes in java, but today an idea came to my mind. What if I have a bunch of annotated and un-annotated classes in package "org.home.junk" (annotated with annotation "@AnnotatedClass") and…
Xeperis
  • 1,449
  • 2
  • 25
  • 41