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
131
votes
6 answers

Java resource as File

Is there a way in Java to construct a File instance on a resource retrieved from a jar through the classloader? My application uses some files from the jar (default) or from a filesystem directory specified at runtime (user input). I'm looking for a…
Mantrum
  • 1,505
  • 2
  • 11
  • 9
124
votes
8 answers

How do I put all required JAR files in a library folder inside the final JAR file with Maven?

I am using Maven in my standalone application, and I want to package all the dependencies in my JAR file inside a library folder, as mentioned in one of the answers here: How can I create an executable JAR with dependencies using Maven? I want my…
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
104
votes
6 answers

Class.forName() vs ClassLoader.loadClass() - which to use for dynamic loading?

When dynamically loading a class, when is it appropriate to use Class.forName("SomeClass"); and when should I use ClassLoader.getSystemClassLoader().loadClass("SomeClass"); Or, are they two ways of doing the same thing?
Zack
  • 6,232
  • 8
  • 38
  • 68
94
votes
3 answers

OSGi, Java Modularity and Jigsaw

So as of yesterday morning I hadn't a clue as to what OSGi even was. OSGi was just some buzzword that I kept seeing cropping up over and over again, and so I finally set aside some time to brush up on it. It actually seems like pretty cool stuff, so…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
94
votes
5 answers

How to use ClassLoader.getResources() correctly?

How can I use ClassLoader.getResources() to find recursivly resources from my classpath? E.g. finding all resources in the META-INF "directory": Imagine something like getClass().getClassLoader().getResources("META-INF") Unfortunately, this does…
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
79
votes
9 answers

When is the static block of a class executed?

I have 2 jars, let's call them a.jar and b.jar. b.jar depends on a.jar. In a.jar, I defined a class, let's call it StaticClass. In the StaticClass, I defined a static block, calling a method named "init" : public class StaticClass { static { …
Leon
  • 8,151
  • 11
  • 45
  • 51
78
votes
3 answers

Java verbose class loading

I am trying to list the order in which the Java class loader is loading my classes. if I use -verbose parameter it will list every single interface/class it loads, including tons of interfaces such as Serializable, exceptions etc. Is there a way to…
Bober02
  • 15,034
  • 31
  • 92
  • 178
77
votes
5 answers

How to load a jar file at runtime

I was asked to build a java system that will have the ability to load new code (expansions) while running. How do I re-load a jar file while my code is running? or how do I load a new jar? Obviously, since constant up-time is important, I'd like to…
Amir Arad
  • 6,724
  • 9
  • 42
  • 49
76
votes
7 answers

Is it possible to dynamically load a library at runtime from an Android application?

Is there any way to make an Android application to download and use a Java library at runtime? Here is an example: Imagine that the application needs to make some calculations depending on the input values. The application asks for these input…
llullulluis
  • 3,472
  • 3
  • 27
  • 30
71
votes
11 answers

Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger

I've got an interesting problem in which the org.apache.log4j.Logger class is not found during runtime. I'm trying to get authorized and that is where it's failing: OAuthAuthorizer oauthAuthorizer = new OAuthAuthorizer(OAUTH_CONSUMER_KEY,…
70
votes
1 answer

Check if class exists in Java classpath without running its static initializer?

If I use try { Class.forName("my.package.Foo"); // it exists on the classpath } catch(ClassNotFoundException e) { // it does not exist on the classpath } the static initializer block of "Foo" is kicked off. Is there a way…
Epaga
  • 38,231
  • 58
  • 157
  • 245
67
votes
5 answers

Flow of class loading for a simple program

I am just now beginning to learn the internal architecture of Java. I have roughly understood the concept of class loading which loads the required classes when jvm runs, ClassNotFoundException is thrown when a class is not found and specific class…
Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
66
votes
2 answers

Load properties file in JAR?

I'm having trouble when one of the jars that my web app depends on tries to load a properties file from within the jar. Here is the code in the jar. static { Properties props = new Properties(); try { …
Andy
  • 8,841
  • 8
  • 45
  • 68
65
votes
9 answers

this.getClass().getClassLoader().getResource("...") and NullPointerException

I have created a minimal maven project with a single child module in eclipse helios. In the src/test/resources folder I have put a single file "install.xml". In the folder src/test/java I have created a single package with a single class that does: …
u123
  • 15,603
  • 58
  • 186
  • 303
65
votes
7 answers

How to deal with LinkageErrors in Java?

Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux. My application, using the Java Plugin Framework, appears unable to convert a dom4j-created XML document to Batik's implementation of the…
Urs Reupke
  • 6,791
  • 3
  • 35
  • 49