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
15
votes
2 answers

Use Absolute path for ClassLoader getResourceAsStream()

I am trying to use ClassLoader getResourceAsStream() My Direcory structure is like below: Project1 -src -main -java -webapp -WEB-INF -MYLOC -someprops.properties For classloader.getResourceAsStream("MYLOC/someprops.properties") works…
user3018487
  • 153
  • 1
  • 1
  • 4
15
votes
2 answers

Dynamically load the JDBC driver

I'm trying to load the JDBC driver dynamically with this kind of code: try{ URL[] url={new URL("file:libs/mysql-connector-java-5.1.21.jar")}; URLClassLoader loader = new URLClassLoader(url, System.class.getClassLoader()); …
bkb
  • 309
  • 2
  • 4
  • 12
14
votes
4 answers

What is an isolated classloader in Java?

When trying to solve this problem, I encountered some articles, etc. referring to "isolated" ClassLoaders. I was unable to find a definition for isolated classloader via Google search, so maybe the term is not widely-known jargon, and perhaps has a…
jyoungdev
  • 2,674
  • 4
  • 26
  • 36
14
votes
2 answers

Why ContextClassLoader returns path with exclamation character?

I try to open file in jar in WEB-INF/lib with Thread.currentThread().getContextClassLoader(); URL url=classLoader.getResource(myconfig); In debugger I can see: jar:file:/C:/apache-tomcat/webapps/mywebapp/WEB-INF/lib/myjarresource.jar! …
user710818
  • 23,228
  • 58
  • 149
  • 207
14
votes
5 answers

How to debug JVM resources loading?

To debug class loading in a JVM we can use the param -verbose:class, but... Anyone knows how to debug resources loading (e.g. properties files)?
aows
  • 526
  • 1
  • 4
  • 10
14
votes
3 answers

Java adding to a unknown type generic list

I've come into something I haven't come across before in Java and that is, I need to create a new instance of say the ArrayList class at runtime without assigning a known type then add data to the list. It sounds a bit vague so here is an…
ars265
  • 1,949
  • 3
  • 21
  • 37
14
votes
1 answer

How to change default class loader in Java?

Let's say I have three classes, example.ClassA, example.ClassB, and example.ClassLoader. ClassA prints out HelloWorld and ClassB imports example.ClassA and invokes its main() method. If I do this: java -cp Example.jar…
Yifan
  • 4,867
  • 5
  • 25
  • 24
14
votes
2 answers

ClassCircularityError thrown by ClassLoader.defineClass

I'm loading classes using a custom class loader. For the most part, everything works, but sometimes when I load particularly complex projects/libraries, I get a strange bug: Exception in thread "main" java.lang.ClassCircularityError: …
Jim
  • 2,111
  • 4
  • 25
  • 34
14
votes
1 answer

Which ClassLoader should I supply to Proxy.newProxyInstance(...)?

I have read the documentation, but I still don't understand which classloader I should supply as an argument. I've tried a few options, but this seems to have no effect on compilation or the behavior of the proxy. It is a little unsettling that I…
jonderry
  • 23,013
  • 32
  • 104
  • 171
14
votes
2 answers

Difference between AppClassloader and SystemClassloader

I am so confused about these two class loaders. When talking about the hierarchy of Java class loaders, usually the bootstrap classloader and ext class loader and the third one (system classloader or app classloader) are mentioned. To be more…
QiKuo Zhang
  • 141
  • 1
  • 1
  • 4
14
votes
2 answers

Why is ClassLoader's cache checked in ascending sequence?

Why is ClassLoader's cache checked in ascending sequence while class-loading befalls in descending sequence?
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
14
votes
2 answers

Is it possible to use instanceof when passing objects between Threads?

I've run into an issue where instanceof works, and then it doesn't. Going into details is difficult, but I think this might be the problem: Reading this: http://www.theserverside.com/news/thread.tss?thread_id=40229 (search for…
Risser
  • 585
  • 7
  • 20
14
votes
6 answers

What is the need of different class loaders in java

I have read that there are different class loaders in java, the one is primordial class loader and there are custom class loaders as well, so I want to understand why primordial class loader is not able to serve all classes in java? Why is there…
Ankit Zalani
  • 3,068
  • 5
  • 27
  • 47
14
votes
1 answer

log4j and the thread context classloader

I'm a newbie to Java and just starting to figure out the concept of class loaders. Right now I am having some issues with log4j regarding its use of the thread context classloader. I'm getting the following errors: A…
Idan K
  • 20,443
  • 10
  • 63
  • 83
14
votes
3 answers

getResourceAsStream from JUnit

I am creating a JUnit TestCase for a project which needs to load a configuration file during initialization. This configuration file is inside the project in src/main/resources/config folder and during the build maven places it into /config folder,…
Carles Sala
  • 1,989
  • 1
  • 16
  • 34