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

Custom Java classloader not being used to load dependencies?

I've been trying to set up a custom classloader that intercepts classes to print out which classes are being loaded into the application. The classloader looks like this public class MyClassLoader extends ClassLoader { @Override public…
Li Haoyi
  • 15,330
  • 17
  • 80
  • 137
17
votes
1 answer

Running an uber jar from sbt assembly results in error: Could not find or load main class

I have a spark job packaged as an uber-jar using the sbt assembly plugin. The build.sbt specifies a runnable main to be the target of the resulting uber-jar mainClass in assembly := Some("com.foo.Bar") After the assembly is correctly created,…
maasg
  • 37,100
  • 11
  • 88
  • 115
17
votes
5 answers

Order of class loading from a .war file

I've got a question regarding the guarantees, if any, in the following scenario (note that the question is not "How to do this in a different way?", the question is really about class loading order in the following case (to better understand how…
SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
17
votes
1 answer

When is a Java Class loaded?

I searched the internet for more than couple of hours and could not reach any conclusion. Recently I decided to use BouncyCastle for SSL but I wanted it to off by default, so that BouncyCastle jar may not be in the class path. private void…
Arun Thirupathi
  • 351
  • 2
  • 11
17
votes
3 answers

Who firstly creates Class object during class-loading process?

In the documentation I found: Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader. I checked the source code, but didn't find the place…
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
17
votes
18 answers

What do people use class loading for?

So, every Java text book talks about how flexible Java is since it can load classes at run time. Just cobble together a string and give it to Class.forName(), and catch the ClassNotFoundException and handle it. So much for the theory. Can you give…
doppelfish
  • 963
  • 7
  • 18
17
votes
3 answers

Is there a way to force a classloader to load a package even if none of its classes have been loaded?

Let's say a java codebase has a package called "com.example". At runtime, we can get this Package by calling Package p = Package.getPackage( "com.example" ); //(returns null) or even get a list of all packages by calling Packages[] ps =…
mpobrien
  • 4,922
  • 1
  • 33
  • 35
17
votes
4 answers

Is there a way to get which classes a ClassLoader has loaded?

I am trying to implement some unit testing for an old framework. I am attempting to mock out the database layer. Unfortunately our framework is a bit old and not quite using best practices so there is no clear separation of concerns. I am bit…
uriDium
  • 13,110
  • 20
  • 78
  • 138
17
votes
4 answers

When do Java class loaders engage?

There's 10 million articles and docs out there on what Java classloaders are, and how/*why* to write your own...but they all seem to be assuming some things that I can't find a simple answer to! I understand the job of the classloader: to read…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
16
votes
3 answers

When does classLoader load imports?

Assume, that I have class with static methods only. Will class loader load every imported class when loading class to memory? Or it will only load imports when a method from this would need access to it? Question is whether class loader loads…
Damian
  • 2,930
  • 6
  • 39
  • 61
16
votes
4 answers

how to share objects between different classloaders?

I need different classloaders to be able to unload classes. But i need to share objects between them (actually i am getting ClassCastException). So what are the solutions to deal with this?. Thanks
Wyvern666
  • 633
  • 1
  • 11
  • 26
16
votes
2 answers

Java: accessing protected fields from inner class

Recently I've faced a problem getting a runtime error java.lang.IllegalAccessError when trying to access from inner class a protected field declared in outer's parent class that was loaded by a different class loader. Briefly: Class Parent has…
Timofei Davydik
  • 7,244
  • 7
  • 33
  • 59
16
votes
3 answers

Order of Tomcat Classloaders: common, shared, and server

The Tomcat Class Loader HOW-TO documentation describes 4 different class loaders: Bootstrap System Common Webapp In the default catalina.properties file, however, there are properties defined for a shared and server class loader as well. In the…
Mike Deck
  • 18,045
  • 16
  • 68
  • 92
16
votes
3 answers

Patch or override an implementation of a core Java 10 class

There is a bug in JFX which often manifests when calculating screen co-ordinates https://bugs.openjdk.java.net/browse/JDK-8194727 and https://bugs.openjdk.java.net/browse/JDK-8190400 I've tracked the problem down to the implementation of…
jambit
  • 313
  • 1
  • 8
16
votes
2 answers

Jython CLASSPATH, sys.path and JDBC drivers

How can I add JDBC drivers at runtime to Jython? Using CLASSPATH works, but using sys.path doesn't work with zxJDBC even though the class is imported fine and can be manipulated from the Jython interpreter prompt. Why does this work: $…
lmz
  • 1,560
  • 1
  • 9
  • 19