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
13
votes
3 answers

What is the reason for having 3 Class loaders in Java

Java has 3 class loaders: BootStrap, Extension and System and they have one role; loading classes from different packages. But why does Java have 3 different class loaders instead of just one, since one class loader can load all the necessary…
GD_Java
  • 1,359
  • 6
  • 24
  • 42
13
votes
2 answers

Strange jboss console error

I'm creating additional module to already multi-module maven project. And for this one I want everything to be like in other modules(meaning dependencies) just to test hello world, then I'll go do some more complex stuff. And it does print hello…
ant
  • 22,634
  • 36
  • 132
  • 182
13
votes
1 answer

Classloader behaviour on Tomcat with multiple applications

On a Tomcat 5.5 server, I put a class in the system classpath (and modify catalina.bat to pick it), or if I put class in the shared lib directory. Now if I have two different applications using the same class which do not have the class in their…
saugata
  • 2,823
  • 1
  • 27
  • 39
13
votes
1 answer

Is the defining classloader of a class level annotation always a parent of the initiating classloader of that class?

Suppose the following: @SomeAnnotation public interface Foo { } I would like to know if it is always the case that either the defining classloader of SomeAnnotation is equal to or a parent of the initiating classloader of Foo. I have read JVMS v8…
SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
13
votes
1 answer

How to test a Class.forName call in Java code?

I've been messing around with ClassLoaders in java recently, trying to test code which uses dynamic loading of classes (using Class.forName(String name)) with a custom ClassLoader. I've got my own custom ClassLoader set up, which is supposed to be…
Volker
  • 133
  • 1
  • 5
13
votes
8 answers

Java: No suitable driver found for jdbc:h2

I'm having an issue with some of my code, I've searched and tried everything I know of, without any luck. Scenario: Application checks if JDBC driver exists, in this case, the H2 driver (org.h2.Driver). If it doesn't exist, the application…
Contex
  • 141
  • 1
  • 1
  • 6
13
votes
3 answers

What is the difference between getResourceAsStream with and without getClassLoader?

I'd like to know the difference between the following two: MyClass.class.getClassLoader().getResourceAsStream("path/to/my/properties"); and MyClass.class.getResourceAsStream("path/to/my/properties"); Thank you.
George Sun
  • 881
  • 1
  • 10
  • 20
12
votes
1 answer

analyze jar file programmatically

I need to count the number of compiled classes, interfaces and enums in a given jar file programmatically (so I need three separate numbers). Which API would help me? (I can't use third party libraries.) I've already tried quite tricky scheme which…
Artem Pelenitsyn
  • 2,508
  • 22
  • 38
12
votes
3 answers

How to get name of main class of a jar file from Java?

I want to load and execute external jar file using URLClassLoader. What is the easiest way to get "Main-Class" from it?
Vi.
  • 37,014
  • 18
  • 93
  • 148
12
votes
2 answers

How do you disable lazy class loading/initialization in Sun's JVM?

By default, Sun's JVM both lazily loads classes and lazily initializes (i.e. calls their methods) them. Consider the following class, ClinitBomb, which throws an Exception during a static{} block. public class ClinitBomb { static { …
jade
  • 744
  • 5
  • 16
12
votes
4 answers

Can I set the classloader policy for WebSphere in the ibm-web-bnd.xmi file?

I have a JEE application that runs on WAS 6. It needs to have the class loader order setting to "Classes loaded with application class loader first", and the WAR class loader policy option set to "Single class loader for application". Is it…
pkaeding
  • 36,513
  • 30
  • 103
  • 141
12
votes
1 answer

Java ServiceLoader explanation

I'm trying to understand the Java ServiceLoader concepts, working mechanism and concrete use cases, but find the official documentation too abstract and confusing. First of all, documentation outlines services and service providers. Service is a…
Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225
12
votes
2 answers

Forcing class load

Is there a way in C# or .net IL to force a class that has a type initializer (static constructor) to load itself, without accessing any of its parameters? Assuming I've got the class public static class LogInitialization { static…
configurator
  • 40,828
  • 14
  • 81
  • 115
12
votes
1 answer

UrlClassLoader delegation and inheritance hierarchy

I have been confused with UrlClassLoader delegation hierarchy and inheritance hierarchy. I created class that extends UrlClassLoader and executed: childOfUrlClassLoader.getParent().getClass().getName() which gave…
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
12
votes
2 answers

Spring jars in tomcat lib folder

I am completely confused over how the class loading happens in tomcat. So please bear with me if my question sounds stupid. We deploy multiple spring webapps on single tomcat server. To reduce the memory footprint, we thought of having spring,…
sidgate
  • 14,650
  • 11
  • 68
  • 119